Why do I receive Index exceeds the number of array elements (1)?
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
John D'Errico
2021-8-25
编辑:John D'Errico
2021-8-25
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.
0 个评论
Awais Saeed
2021-8-25
编辑:Awais Saeed
2021-8-25
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 个评论
John D'Errico
2021-8-25
编辑:John D'Errico
2021-8-25
Um, no. That is not the answer. Well, let me say that your answer misses the problem.
Awais Saeed
2021-8-25
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!