Array indices must be positive integers or logical values.
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
george veropoulos
le 25 Nov 2024 à 16:31
Modifié(e) : Cris LaPierre
le 25 Nov 2024 à 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
0 commentaires
Réponse acceptée
Cris LaPierre
le 25 Nov 2024 à 16:46
Modifié(e) : Cris LaPierre
le 25 Nov 2024 à 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)
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!