function [cs,tr]=nmaChainMarkov(p) %function [cs,tr]=nmaChainMarkov(p) % %By Nasser Abbasi %March 8, 2008 % [nRow,nCol]=size(p); if nRow~=nCol error 'matrix must be square'; end lhs=1:nCol; rhs=cell(nRow,1)'; for i=1:nCol rhs{i}=find(p(i,:)~=0); end keepOn=1; while keepOn newRhs=updateRhs(lhs,rhs); if isequal(newRhs,rhs) keepOn=0; else rhs=newRhs; end end theClasses=findSets(lhs,newRhs); [cs,tr]=classifyClasses(theClasses); end %%%%%%%%%%%%%%%%%%%%%%%%%%%% % called after all classes are found % this function classify the class as closed or % transient %%%%%%%%%%%%%%%%%%%%%%%%%%%% function [cs,tr]=classifyClasses(c) n=length(c); cs=cell(0); tr=cell(0); csIndex=0; trIndex=0; for i=1:n if isequal( c{i}(1), c{i}(2) ) csIndex=csIndex+1; cs{csIndex}(1)=c{i}(1); cs{csIndex}(2)=c{i}(2); else trIndex=trIndex+1; tr{trIndex}(1)=c{i}(1); tr{trIndex}(2)=c{i}(2); end end end %%%%%%%%%%%%%%%%%%%%%%%%%%%% % updated the RHS lists, see paper. % %%%%%%%%%%%%%%%%%%%%%%%%%%%% function newRhs=updateRhs(lhs,rhs) for i=1:length(lhs) n=0; for j=1:length(rhs{i}) if rhs{i}(j)==lhs(i) n=n+1; tmp{i}(n)=lhs(i); else n=n+1; tmp{i}(n)=rhs{i}(j); z=rhs{i}(j); for k=1:length(rhs{z}) n=n+1; tmp{i}(n)=rhs{z}(k); end end end tmp{i}=unique(tmp{i}); end newRhs=tmp; end %%%%%%%%%%%%%%%%%%%%% % find all the states with the same rhs % %%%%%%%%%%%%%%%%%%%%%% function theClasses=findSets(lhs,rhs) n=length(lhs); skip=cell(0); nskip=0; skip{1}(1)=0; entryNumber=0; for i=1:n if isempty(find(skip{1}==i)) entryNumber=entryNumber+1; nskip=nskip+1; skip{1}(nskip)=i; tmp=cell(2,1); k=1; tmp{1}(k)=i; tmp{2}=rhs{i}; for j=1:n if j ~= i if isempty(find(skip{1}==j)) if isequal(tmp{2},rhs{j}) k=k+1; tmp{1}(k)=j; nskip=nskip+1; skip{1}(nskip)=j; end end end end if ~isempty(tmp) theClasses{entryNumber}=tmp; end end end end