Array indices must be positive integers or logical values.

조회 수: 1 (최근 30일)
george veropoulos
george veropoulos 2024년 11월 25일
편집: Cris LaPierre 2024년 11월 25일
Hi i have define the function
function y=triangle_basisn(phi,kk)
%[f,N,ra,k0,Z0,lambda] = parameter();
N=40
dftm=2.*pi./(N-1);
for jj = 1:N
Phi0(jj)=(jj-1).*dftm;
end
Phin=Phi0;
if ( phi >= Phin(kk-1) ) & ( phi <=Phin(kk))
y=(phi-Phin(kk-1))./dftm
elseif (phi >= Phin(kk) ) & (phi <=Phin(kk+1))
y=(Phin(kk+1)-phi)./dftm
else
y=0
end
end
When i call the funcrion i receive the message
Array indices must be positive integers or logical values.
i thni the problem is the tem kk-1 when kk=1 the matlab cant define zero index ?
there a way to ovecomne the problem ?
thank
George

채택된 답변

Cris LaPierre
Cris LaPierre 2024년 11월 25일
편집: Cris LaPierre 2024년 11월 25일
I can reproduce the error if I set kk=1. In this case, Phin(kk-1) becomes Phin(0), which is causing the error. In MATLAB, the index cannot be 0.
y=triangle_basisn(10,1)
N = 40
Array indices must be positive integers or logical values.

Error in solution>triangle_basisn (line 11)
if ( phi >= Phin(kk-1) ) & ( phi <=Phin(kk))
function y=triangle_basisn(phi,kk)
%[f,N,ra,k0,Z0,lambda] = parameter();
N=40
dftm=2.*pi./(N-1);
for jj = 1:N
Phi0(jj)=(jj-1).*dftm;
end
Phin=Phi0;
if ( phi >= Phin(kk-1) ) & ( phi <=Phin(kk))
y=(phi-Phin(kk-1))./dftm
elseif (phi >= Phin(kk) ) & (phi <=Phin(kk+1))
y=(Phin(kk+1)-phi)./dftm
else
y=0
end
end
One way to overcome this is to define an if condition for when kk=1. What the corresponding code does would be up to you, but it would prevent the error. Here's an example:
if kk==1
y=phi;
elseif( phi >= Phin(kk-1) ) & ( phi <=Phin(kk))
y=(phi-Phin(kk-1))./dftm
elseif (phi >= Phin(kk) ) & (phi <=Phin(kk+1))
y=(Phin(kk+1)-phi)./dftm
else
y=0
end

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by