Array indices must be positive integers or logical values.
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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)
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!