Error showing as "index exceeds the number of array elements (3)"

3 vues (au cours des 30 derniers jours)
Biswajit Das
Biswajit Das le 12 Juin 2019
Commenté : James Browne le 12 Juin 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

Réponse acceptée

James Browne
James Browne le 12 Juin 2019
Modifié(e) : James Browne le 12 Juin 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~
  2 commentaires
Biswajit Das
Biswajit Das le 12 Juin 2019
Thank you...
James Browne
James Browne le 12 Juin 2019
You bet!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by