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

2 次查看(过去 30 天)
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

采纳的回答

Daniel Shub
Daniel Shub 2012-3-4
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 个)

Wayne King
Wayne King 2012-3-4
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.

类别

Help CenterFile Exchange 中查找有关 Markov Chain Models 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by