how to fix this error 'Index in position 2 exceeds array bounds.'?

1 回表示 (過去 30 日間)
NURSYAFIKAH MARZUKI
NURSYAFIKAH MARZUKI 2021 年 6 月 9 日
コメント済み: Joel Lynch 2021 年 6 月 22 日
I have a problem with this line of code B(:,g)=Popbest(:,iBest); fbest(g)= Fitbest(iBest); I tried it a lot and changed the numbers, but the error remains. The Error is Index in position 2 exceeds array bounds.
I hope someone has a solution to this problem.

採用された回答

Joel Lynch
Joel Lynch 2021 年 6 月 9 日
編集済み: Joel Lynch 2021 年 6 月 9 日
The proximate reason for the error is that "Popbest" is empty, and so acessing the first column by Popbest(:,1) throws an error.
The most likely root cause is that the two if statements on lines 158 and 162 prevent "Popbest" from being defined in certain cases. A good approach is to make sure it has a consistent size set before the if statements, then set an alternative value in else branches.
  4 件のコメント
NURSYAFIKAH MARZUKI
NURSYAFIKAH MARZUKI 2021 年 6 月 22 日
Here is the current script.
Joel Lynch
Joel Lynch 2021 年 6 月 22 日
The function effi does not appear to do anything, can you describe how you want it to work? currently effi just returns the division of global constants Z=N=W/Q.
Your plots are meant to plot rows of data in B
plot(1:GEN,B(1,:)); plot(1:GEN,B(2,:)); plot(1:GEN,B(3,:));
but your main for loop over "g" appends a column for each g. However the data in Popbest is always a single scalar, so you end up with a row vector B(1:GEN,1), that never has more than one row.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by