function T = transition_matrix ( A );

%*****************************************************************************80
%
%% transition_matrix computes the transition matrix for an incidence matrix.
%
%  Discussion:
%
%    The transition matrix is essentially the transpose of the incidence
%    matrix, with each column normalized so that it sums to 1.
%
%    In cases where row i of the incidence matrix is 0, the corresponding
%    column of the transition matrix have A(i,i) = 1.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    01 March 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A(N,N): the incidence matrix.
%
%  Output:
%
%    real T(N,N): the transition matrix.
%
  n = size ( A, 1 );
%
%  Allocate T.
%
  T = A;
%
%  Sum each row.
%
  rowsum = sum ( T, 2 );
%
%  Normalize each row to sum to 1.
%
  for i = 1 : n
    if ( rowsum(i) == 0.0 )
      T(i,i) = 1.0;
    else
      T(i,:) = T(i,:) / rowsum(i);
    end
  end
%
%  Transpose so the matrix is a transition/stochastic matrix.
%
  T = T';

  return
end

