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

3 ビュー (過去 30 日間)
Biswajit Das
Biswajit Das 2019 年 6 月 12 日
コメント済み: James Browne 2019 年 6 月 12 日
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

採用された回答

James Browne
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~
  2 件のコメント
Biswajit Das
Biswajit Das 2019 年 6 月 12 日
Thank you...
James Browne
James Browne 2019 年 6 月 12 日
You bet!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by