%% Identify Communicating Classes of Markov Chain
% Consider this theoretical, right-stochastic transition matrix of a stochastic process.
% P = \left[ {\begin{array}{*{20}{c}}
% {0.5}&{0.5}&0&0\\
% {0.5}&0.4&{0.1}&0\\
% 0&0&0&1\\
% 0&0&1&0
% \end{array}} \right].
% Create the Markov chain that is characterized by the transition matrix P.

P = [0.5 0.5 0 0; 0.5 0.4 0.1 0; 0 0 0 1; 0 0 1 0];
mc = dtmc(P);

%
% 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);

% States 3 and 4 belong to a communicating class with period 2. States 1 and 2 are transient.
%
% Programmatically identify to which communicating classes the states belong.

bins = classify(mc)

% bins is a 1-by-4 vector of communicating class labels. Elements of bins correspond to the states in mc.StateNames. For example, bins(3) = 2 indicates that state 3 is in communicating class 2.
% Copyright 2012 The MathWorks, Inc.