Array indices must be positive integers or logical values.

2 次查看(过去 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

采纳的回答

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