Débutant en matlab : problème index must be logical

Résolu/Fermé
DoubelV Messages postés 1 Date d'inscription vendredi 30 mai 2014 Statut Membre Dernière intervention 30 mai 2014 - 30 mai 2014 à 20:31
 DoubleV - 2 juin 2014 à 22:13
Bonjour, je débute sur Matlab et essai de faire un petit programme : je posséde 100 valeurs notées theta(n) où n varie de 1 à 100. Je souhaite définir une fonction g telle que g(theta(n))=1 si theta(n) appartient à un certain intervalle, 0 sinon. Voici mon programme :
for n=1:100
theta(n)=(pi/2)-n*pi/100;
if (theta(n)<-0.5 || theta(n)>0.5)
g(theta(n))=0;
else g(theta(n))=1;
end
end

Je cherche l'erreur mais ne la trouve pas... Matlab m'affiche : Attempted to access (1.53938); index must be a positive integer or logical.

Merci d'avance pour vos suggestions!


2 réponses

JulienJust Messages postés 139 Date d'inscription mardi 25 juin 2013 Statut Membre Dernière intervention 2 septembre 2014 18
2 juin 2014 à 14:35
Bonjour,

Essaie plutôt ceci:

N = 100;
g = zeros(1,N);

for n = 1:N
theta(n) = (pi/2)-n*pi/100;
if theta(n) < -0.5
g(n) = 0;
elseif theta(n) > 0.5
g(n) = 0;
else
g(n) = 1;
end
end

Cdlt
0
Merci pour votre aide!
0