Error showing as "index exceeds the number of array elements (3)"
3 次查看(过去 30 天)
显示 更早的评论
LB=[0 0 0];
UB=[10 10 10];
m=4;
n=100;
for i=1:n
for j=1:m
x0(i,j)=round(LB(j)+rand()*(UB(j)-LB(j)));
end
end
x=x0; % initial population
v=0.1*x0; % initial velocity
for i=1:n
f0(i,1)=fprofit(x0(i,:),1);
end
0 个评论
采纳的回答
James Browne
2019-6-12
编辑:James Browne
2019-6-12
Greetings,
Without knowing what you are trying to accomplish with your code, I couldn't really begin to fix it for you but I can tell you tha the problem is coming from the following line of code:
x0(i,j)=round(LB(j)+rand()*(UB(j)-LB(j)));
I can also tell you that the problem is happening because both variables "UB" and "LB" are vectors with 3 elements. This means that for both variables, the range of legal index values is 1-3;
In your code, the second for loop iteration ranges ( "j" ) eceeds the legal range of the index values for "UB" and "LB". I can also tell you that the problem occurs when the second for loop iterator, "j", reaches 4.
If you change the value of "m" from 4 to 3, this would fix your problem, as long as "m" does not need to be a value of 4 for some other reason.
If you can describe what you are trying to accomplish, I or someone else may be able to help you get there.
Hope that helps~
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!