Azzera filtri
Azzera filtri

Why this piece of code gives error?

2 visualizzazioni (ultimi 30 giorni)
The following piece of code gives error:
for i=1:size(Positions,1)
Fit(i) = fobj(Positions(i,:));
The error it gives is as:
Array indices must be positive integers or logical values.
Error in fun4sn0 (line 18)
Error in abc (line 4)
Fit(i) = fobj(Positions(i,:));

Risposta accettata

Walter Roberson
Walter Roberson il 16 Ott 2022
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);
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
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.

Più risposte (0)


Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by