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

조회 수: 265 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by