%% Create Markov Chain Using Matrix of Observed Transition Counts
% Consider this transition matrix in which element (i,j) is the observed number of times state i transitions to state j.
% P = \left[ {\begin{array}{*{20}{c}} {16}&2&3&{13}\\ 5&{11}&{10}&8\\
% 9&7&6&{12}\\ 4&{14}&{15}&1 \end{array}} \right].
% For example, P_{32} = 7 implies that state 3 transitions to state 2 seven times.

P = [16 2  3  13;
     5  11 10 8;
     9  7  6  12;
     4  14 15 1];

%
% Create the Markov chain that is characterized by the transition matrix P.

mc = dtmc(P);

%
% Display the normalized transition matrix stored in mc. Verify that the elements within rows sum to 1 for all rows.

mc.P
sum(mc.P,2)

%
% Plot a directed graph of the Markov chain.

figure;
graphplot(mc);

% Copyright 2012 The MathWorks, Inc.