MATLAB: Error: Subscript indices must either be real positive integers or logicals.

error

I have the following code and I am having this error
Subscript indices must either be real positive integers or logicals.
Error in ==> Markov_Chain at 64
cur_state = Rand_Vect(P(cur_state,:), 1);
How should 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,1:2) = cur_state;
for i = 1:n
cur_state = Rand_Vect(P(cur_state,:), 1);
Chain(i,1:2) = cur_state;
end

Best Answer

Look at the error and then look at your code. Basically you have
cur_state = round([0.4 0.6]);
which is the same as
cur_state = [0, 1];
Now the error says "real positive integers or logicals". Since cur_state is not a logical array, all of its elements must be real positive integers, but 0 is not a positive integer.
Related Question