MATLAB Answers

Why do I receive Index exceeds the number of array elements (1)?

13 views (last 30 days)
Raymond Tsao
Raymond Tsao on 25 Aug 2021
Edited: Awais Saeed on 25 Aug 2021
close all
clear all
clc
tend = 10;
k = 0.1;
alpha = 0.09;
beta = 0.4;
gamma = 0.08;
t(1) = 0;
L(1) = 10;
R(1) = 10;
C(1) = 0;
i = 1;
while t(end) < tend
props = zeros(1,4);
props(1) = k * L(i) * R(i);
props(2) = alpha * L(i);
props(3) = beta * R(i);
props(4) = gamma * C(i);
prop_sum = sum(props);
r1 = rand(1);
tau = (1/prop_sum) * log(1/r1);
t(i+1) = t(i) + tau;
r2 = rand(1);
if r2 * prop_sum <= props(1)
L(i+1) = L(i) - 1;
R(i+1) = R(i) - 1;
C(i+1) = C(i) - 1;
else if r2 * prop_sum > props(1) && r2 * prop_sum <= props(1)+props(2)
L(i+1) = L(i) - 1;
else if r2 * prop_sum > props(1)+props(2) && r2 * prop_sum <= props(1)+props(2)+props(3)
R(i+1) = R(i) - 1;
else if r2 * prop_sum > props(1)+props(2)+props(3) && r2 * prop_sum <= props(1)+props(2)+props(3)+props(4)
C(i+1) = C(i) - 1;
end
end
end
end
i = i + 1;
end
plot(t,L,Y,C)
After I run the program, the command window shows
"Index exceeds the number of array elements (1).
Error in untitled4 (line 20)
props(1) = k * L(i) * R(i);"
What is the problem and how should I fix this error?

Answers (2)

John D'Errico
John D'Errico on 25 Aug 2021
Edited: John D'Errico on 25 Aug 2021
You would be best served to learn to use the debugger. But I can see what happens.
At the first iteration, i = 1. And all of your variables are initially scalars, thus L, T, R, and C.
But inside the while loop, you add new elements to those variables, that were once scalars. But you ONLY append a new element under a specific condition. Thus, we see:
if r2 * prop_sum <= props(1)
What happens if that test fails?
If it fails, then L and C and P and R are NOT expanded.
But then you increment i. And you ALWAYS increment i on EVERY iteration. So what happens after the first iteration? i is now 2. But if that test failed, then those variables are still scalars. And on the second iteration, you try to access L(2), and C(2), and R(2).
Worse, you will continue to have this problem in the future for later iterations, every time that test fails. And since the test is dependent on a random value in r2, it may fail often enough.
So I have no idea what is your goal in writing this code. But it has a bug built into it. You need to rethink what you are doing.

Awais Saeed
Awais Saeed on 25 Aug 2021
Edited: Awais Saeed on 25 Aug 2021
Your L = 10(a scaler). After second iteration, i = 2 and L(2) does not exist. That is why you are seeing that error. Secondly, only a single if statement will execute per iteration and only one element will be appended to either L, R, or C. Which means if an element is added to R(3), then L and C remain the same and you cannot access L(3) and C(3) because they does not exist.
  2 Comments
Awais Saeed
Awais Saeed on 25 Aug 2021
I completely meant what you said. I just made it short assuming that the OP wrote the code and he will pick up the point by hinting to the problem.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!