Why this piece of code gives error?

조회 수: 1 (최근 30일)
Sadiq Akbar
Sadiq Akbar 2022년 10월 16일
댓글: Sadiq Akbar 2022년 10월 16일
The following piece of code gives error:
Positions=rand(30,3);
fobj=@fun4sn0;
for i=1:size(Positions,1)
Fit(i) = fobj(Positions(i,:));
end
The error it gives is as:
Array indices must be positive integers or logical values.
Error in fun4sn0 (line 18)
xo(1,k)=xo(1,k)+u(i)*exp(-1i*(k-1)*pi*cosd(u(P+i)));
Error in abc (line 4)
Fit(i) = fobj(Positions(i,:));
  댓글 수: 1
KSSV
KSSV 2022년 10월 16일
Show us your function @fun4sn0

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2022년 10월 16일
Positions=rand(30,3);
Three columns
Fit(i) = fobj(Positions(i,:));
You pass in one row at a time, so you pass in a vector with one row and three columns.
function e=fun4sn0(pop)
where it is received as pop
C = size(pop,2);
P=C/2;
3 columns so C is 3 and P is 1.5
for i=1:P
i is 1:1.5 which will be just 1
xo(1,k)=xo(1,k)+u(i)*exp(-1i*(k-1)*pi*cos(u(P+i)));
P is 1.5 so P+i will be 2.5 and you will be indexing u at 2.5 which is not a valid index
u=[25 45 ];% desired vector
You hard-coded u to be length 2 but you can see that you are indexing u according to the size of the input vector, which will be a problem if the input gets larger.
  댓글 수: 1
Sadiq Akbar
Sadiq Akbar 2022년 10월 16일
Thanks a lot for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Chassis Systems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by