error in the code for markov chain
Show older comments
I am trying to simulate a markov chain with the following code. I keep on having this error:
In an assignment A(I) = B, the number of elements in B and I must be the same. Error in ==> Markov_Chain at 61 Chain(1) = cur_state;
How can I solve it? thanks
function [Chain] = Markvok_Chain( P, initial_state, n )
P = [ 0.85 0.15; 0.05 0.95 ];
initial_state = [0.4 0.6];
n=20;
sz = size(P);
cur_state = round(initial_state);
% % Verify that the input parameters are valid % if (sz(1) ~= sz(2)) error('Markov_Chain: Probability matrix is not square'); end num_states = sz(1);
if (cur_state < 1) | (cur_state > num_states) error('Markov_Chain: Initial state not defined in P') end
for i=1:num_states if (sum(P(i,:)) ~=1 ) error('Markov_Chain: Transition matrix is not valid') end end
% % Create the Markov Chain
Chain(1) = cur_state; for i = 1:n
cur_state = Rand_Vect(P(cur_state,:), 1);
Chain(i) = cur_state;
Accepted Answer
More Answers (0)
Categories
Find more on Markov Chain Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!