Error showing as "index exceeds the number of array elements (3)"
6 views (last 30 days)
Show older comments
Biswajit Das
on 12 Jun 2019
Commented: James Browne
on 12 Jun 2019
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 Comments
Accepted Answer
James Browne
on 12 Jun 2019
Edited: James Browne
on 12 Jun 2019
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~
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!