필터 지우기
필터 지우기

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

조회 수: 4 (최근 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~

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by