MATLAB: Two state markov chain realization

doit4mehomeworkmarkov chainprobabilitystatistics

I have a state transition probability matrix and a state probability vector
[0.9 0.1; 0.1 0.9] & [0.4 0.6] respectively.
Now, I want to generate the states according to this. say 100 state sequence.
Any sort of help would be appreciated.
Thanks.

Best Answer

I have to admit that my memory of MC is a bit rusty. Does this do what you want? Do you expect a convergence to a state probability vector [0.5 0.5]?
If this is right, there are faster implementations, but I wanted to lay out the basics clearly.
transitionMatrix = [0.9 0.1; 0.1 0.9];
initialProbabilityState = [0.4; 0.6]; % Made it a column vector, rather than a row.
nStates = 100;
states = zeros(2,nStates);
states(:,1) = initialProbabilityState;
for ns = 2:nStates
states(:,ns) = transitionMatrix*states(:,ns-1);
end