"dpr" wrote in message <jivuh4$o2c$1@newscl01ah.mathworks.com>...
> 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;
>
> end
        
As to your error message, you are trying to put a 1 by 2 array into a single element of 'Chain'. It should read
Chain(i,:) = cur_state;
and 'Chain' should be defined as an num_states by 2 array.
I see other questionable aspects of your code also. For example your 'round' of 'initial_state' would convert it to a one and a zero which seems to me to totally defeat the purpose of your code. Why would you do that?
Second, the condition "(cur_state < 1)  (cur_state > num_states)" is being used in an 'if' statement, but that condition as it stands is a 1 by 2 boolean array and 'if' doesn't handle multiplevalued arrays in the way you would expect. It should have an 'any' or 'all' or the like to reduce it to a single scalar boolean. Also it doesn't make any sense  why would you worry about probability values being in excess of num_states?
Third, I don't know what 'Rand_Vect' does, but somewhere in your code you should be carrying out a matrix multiplication of 'cur_state' by P to simulate a valid Markov chain, and when you do, 'cur_state' should be a column vector.
Roger Stafford
