Array indices must be positive integers or logical values.

19 ビュー (過去 30 日間)
george veropoulos
george veropoulos 2024 年 11 月 25 日 16:31
編集済み: Cris LaPierre 2024 年 11 月 25 日 16:49
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 日 16:46
編集済み: Cris LaPierre 2024 年 11 月 25 日 16:49
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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by