Home > wafo > markov > mat2tmat.m

mat2tmat

PURPOSE ^

Converts a matrix to a transition matrix.

SYNOPSIS ^

P = mat2tmat(F,def,K)

DESCRIPTION ^

  MAT2TMAT  Converts a matrix to a transition matrix.
 
  CALL: P = mat2tmat(F)
        P = mat2tmat(F,def)
        P = mat2tmat(F,def,Nsub)
 
  P   = Transition matrix            [nxn]
 
  F   = Matrix                    [nxn]
  def =  0: Markov chain transition matrix. (default)
         1: min-Max transition matrix.
            (Zeros on and below the diagonal.)
        -1: Max-min transition matrix.
            (Zeros on and above the diagonal.)
  K   = 'if def=1' : Set zeros below the K-th diagonal.
        'if def=-1': Set zeros above the K-th diagonal.
        'if def=0' : Not applicable.
 
  The routine converts a matrix to a transition matrix, 
  i.e. normalizes each row sum to 1.
 
  Example: 
    F = magic(5)
    mat2tmat(F)
    mat2tmat(F,1)
    mat2tmat(F,-1)
    mat2tmat(F,-1,-3)
    mat2tmat(F,-1,0)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

001 function P = mat2tmat(F,def,K)
002 % MAT2TMAT  Converts a matrix to a transition matrix.
003 %
004 % CALL: P = mat2tmat(F)
005 %       P = mat2tmat(F,def)
006 %       P = mat2tmat(F,def,Nsub)
007 %
008 % P   = Transition matrix            [nxn]
009 %
010 % F   = Matrix                    [nxn]
011 % def =  0: Markov chain transition matrix. (default)
012 %        1: min-Max transition matrix.
013 %           (Zeros on and below the diagonal.)
014 %       -1: Max-min transition matrix.
015 %           (Zeros on and above the diagonal.)
016 % K   = 'if def=1' : Set zeros below the K-th diagonal.
017 %       'if def=-1': Set zeros above the K-th diagonal.
018 %       'if def=0' : Not applicable.
019 %
020 % The routine converts a matrix to a transition matrix, 
021 % i.e. normalizes each row sum to 1.
022 %
023 % Example: 
024 %   F = magic(5)
025 %   mat2tmat(F)
026 %   mat2tmat(F,1)
027 %   mat2tmat(F,-1)
028 %   mat2tmat(F,-1,-3)
029 %   mat2tmat(F,-1,0)
030 
031 % Tested  on Matlab  5.3
032 %
033 % History:
034 % Revised by PJ  23-Nov-1999
035 %   updated for WAFO
036 % Created by PJ (Pär Johannesson) 1998
037 %   from 'Toolbox: Rainflow Cycles for Switching Processes V.1.1'
038 
039 % Check input arguments
040 
041 ni = nargin;
042 no = nargout;
043 error(nargchk(1,3,ni));
044 
045 if ni == 1
046   def = 0;
047 end
048 if ni < 3
049   K = def;
050 end
051 
052 n = length(F);  % Number of levels
053 
054 if def == 0  % Transition matrix for Markov chain
055   
056   P = F;
057   % Set negative elements to zero
058   [I,J] = find(P<0);
059   if length(I) ~= 0
060     warning(['Negative elements in F. Setting to zero!']);
061     for k = 1:length(I)
062       P(I(k),J(k)) = 0;
063     end
064   end
065 
066   sumP = sum(P,2);
067   % Normalize rowsums
068   if sum(sumP == 1) ~= n
069     for i = 1:n
070       if sumP(i)~=0
071     P(i,:) = P(i,:)/sumP(i); 
072       end
073     end
074   end
075   
076 elseif def == 1 % min-Max transition matrix 
077   
078   P = triu(F,K); % Set zeros on and below diagonal
079   % Set negative elements to zero
080   [I,J] = find(P<0);
081   if length(I) ~= 0
082     warning(['Negative elements in F. Setting to zero!']);
083     for k = 1:length(I)
084       P(I(k),J(k)) = 0;
085     end
086   end
087 
088   sumP = sum(P,2);
089   % Normalize rowsums
090   N = min([n-K n]); % Number of sums that should be equal to 1.
091   if sum(sumP == 1) ~= N
092     for i = 1:N
093       if sumP(i)~=0
094     P(i,:) = P(i,:)/sumP(i); 
095       end
096     end
097   end
098   
099 elseif def == -1 % Max-min transition matrix 
100   
101   P = tril(F,K); % Set zeros on and above diagonal
102   % Set negative elements to zero
103   [I,J] = find(P<0);
104   if length(I) ~= 0
105     warning(['Negative elements in F. Setting to zero!']);
106     for k = 1:length(I)
107       P(I(k),J(k)) = 0;
108     end
109   end
110 
111   sumP = sum(P,2);
112   N = min([n+K n]);
113   if sum(sumP == 1) ~= N
114     for i = n-N+1:n
115       if sumP(i)~=0
116         P(i,:) = P(i,:)/sumP(i); 
117       end
118     end
119   end
120   
121 else
122   
123   error(['Undefined definition: def = ' num2str(def) ]);
124   
125 end
126

Mathematical Statistics
Centre for Mathematical Sciences
Lund University with Lund Institute of Technology

Comments or corrections to the WAFO group


Generated on Thu 06-Oct-2005 02:21:16 for WAFO by m2html © 2003