%% Extract Recurrent Subchain of Markov Chain
% Identify the communicating classes of a Markov chain. Then, extract any recurrent subchains from the Markov chain.
% Generate a random seven-state Markov chain. Specify that 40 random elements in the transition matrix should be zero.

rng(1); % For reproducibility
mc = mcmix(7,'Zeros',40);

%
% Identify all communicating classes in the Markov chain and whether the classes are recurrent.

[bins,~,ClassRecurrence] = classify(mc);
recurrentClass = find(ClassRecurrence,1) 
recurrentState = find((bins == recurrentClass))

% Class 6, composed of states 1 and 3, is the only recurrent class in mc.
%
% Create a subchain from class 6 by specifying at least one state in the subchain. Plot a digraph of the subchain.

sc = subchain(mc,recurrentState);

figure;
graphplot(sc,'ColorNodes',true);

% Copyright 2012 The MathWorks, Inc.