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

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

 Accepted 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.

1 Comment

Thanks, I'm getting started with matlab. Any ideas on how to make the code work?

Sign in to comment.

More Answers (1)

cur_state is a row vector and the first element is zero. You cannot address the 0-th row or column (element) in MATLAB.
x = rand(10,1);
x(0)
produces the error you are getting.

Categories

Find more on Simulink in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!