%% Determine Class Structure of Markov Chain
% Identify the communicating classes of a Markov chain. Then, determine whether the classes are recurrent and their periodicity.
% 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);

%
% Plot a directed graph of the Markov chain. Visually identify the communicating class to which each state belongs by using node colors.

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

%
% Identify the communicating classes in mc, and then determine:
% The communicating class to which each state belongs
% Whether each communicating class is recurrent
% The period of each class

[bins,ClassStates,ClassRecurrence,ClassPeriod] = classify(mc)

% mc has seven classes. Each state is its own communicating class, except states 1 and 3, which together compose class 6. Class 6 is the only recurrent class; classes 1 through 5 are transient. Class 6 has period 2; all other classes are aperiodic.
% Copyright 2012 The MathWorks, Inc.