Using logical AND in an if statement?
Afficher commentaires plus anciens
Hi, so I'm working on a problem and have an if statement nested in a for-loop. Basically I can put
if x >= -12
plot(x)
end
and my program will work, but when I add the second statement
if x >= -12 & x < -3
plot(x)
end
Nothing happens at all. I can put the statement
x >= -12 & x < -3
in the console it will give me the values, but I just don't understand why it isn't functioning properly in my if-statement.
Here's the relevant code:
for i = -12 : .5 : 12
x = [-12: .5: 12];
if x >= -12 & x < -3
plot(x)
else
fprintf('error ');
end
end
Also if someone could point me in the right direction on using my for-loop index instead creating variable x with the same values that would be great. Just not too sure on how to make it save each value in a matrix.
Using R2015a
EDIT: It seems that evaluations when x < -3 is causing the issue. I still don't know why. I've tried maxing x > -3, x >=-3, and x <= -3 and I still am not getting any output except for my error message.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!