How to fix this error: Attempted to access x(84); index out of bounds because numel(x)=83.
Show older comments
I've been trying to write some code that mimics a Markov Model.
Code:
for i = 2:steps
if ((x(i-1) == 15) || (x(i-1) == -15))
if (x(i-1) == 15)
if (randsign(px) == 1)
x(i) = 15;
elseif (randsign(px) == -1)
x(i) = 14;
end
end
if (x(i-1) == -15)
%randsign(px);
if (randsign(px) == 1)
x(i) = -14;
elseif (randsign(px) == -1)
x(i) = -15;
end
end
else
x(i) = x(i-1) + randsign(px);
end
end
The "randsign" function randomly spits out either -1 or 1 and the boundaries lie between -15 and 15. Once the matrix hits 15 or -15 the code states that the next matrix value should either stay at 15 or -15, respectively, or move. I've tried initializing the matrix with zeros first (x=zeros(1,steps)), but I would observe something odd such that when the matrix value eventually gets to 15 or -15, the following index is skipped...
This is also shown here (after creating a matrix of zeros):
Columns 307 through 324
13 14 15 *15 0* -1 0 -1 0 1 0 1 0 -1 0 -1 -2 -3
I would really appreciate the help if you could give me some advice on how to solve this issue... Thanks!
Accepted Answer
More Answers (0)
Categories
Find more on Whos 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!