how can i fix this error "Array indices must be positive integers or logical values"?

266 ビュー (過去 30 日間)
knowing that z2 is a function that determines the angles, vq2 are velocities determined with interpolation, d are reference angles and vel_thr are fixed speeds in the following for loop this error is thrown " Array indices must be positive integers or logical values".
x = [0 157 670 1255]; %distance
v = [0 7 30 70]; %velocity
xq = z1; %distance
vq2 = interp1(x,v,xq,'linear');
d= [-161.42 -125.58 -89.74 -53.91 -18.07 17.76 53.59 89.43 125.27 161.11];
vel_thr= [5 6 15 25 30 35 45 55 60 70];
for i=1:length(z2);
angsel=z2(i)*180/pi;
[tmp1 icol1]=min(abs(angsel-d));
[tmp2 icol2]=min(abs(360+angsel-d));
if tmp1<tmp2
icol(i)=icol1;
else
icol(i)=icol2;
end
velsel=vq2(i);
itmp=find(velsel>=vel_thr);
irow(i)=itmp(end);
prob(i)=CF20(10+irow(i),1+icol(i));
end
CF20 is a 20x11 matrix of numbers all greater than or equal to zero. i tried to insert these values both with an excel file and with a txt file, but in both cases it doesn't work.
how can I solve this problem?

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 10 月 22 日
編集済み: Cris LaPierre 2021 年 10 月 22 日
The error means that you are either using a number less that 1, or a decimal number to index a variable. As the error says, your index values must be positive integers or logicals.
With your code, I suspect icol(i) is not always an integer.
angsel=z2(i)*180/pi;
[tmp1 icol1]=min(abs(angsel-d));
Below is a simple example to explain the error
A = 1:5;
% Works
A(1)
ans = 1
% Doesn't work
A(1.5)
Array indices must be positive integers or logical values.

その他の回答 (1 件)

Tewachew
Tewachew 2024 年 3 月 17 日
Array indices must be positive integers or logical values.
Error in load_flow8 (line 15)
Pg(round(DG(n + Num_DG),0)) = Pg(round(DG(n + Num_DG),0)) + Ps;
what is the error of this code????

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by