and if in matlab?

2 vues (au cours des 30 derniers jours)
darksideofthemoon101
darksideofthemoon101 le 8 Avr 2011
Hi,
I want to run an if loop with 2 conditions, but I can't see an "and-if" equivalent.
Eg:
if 0<x<1
*and y2<y1*
z=y2;
else
end
What operator can I use instead of and?
Thanks,
Richard
  1 commentaire
Walter Roberson
Walter Roberson le 8 Déc 2011
There is no such thing as an if loop.

Connectez-vous pour commenter.

Réponse acceptée

Sean de Wolski
Sean de Wolski le 8 Avr 2011
&
&&
and(x,y)
doc and
  1 commentaire
darksideofthemoon101
darksideofthemoon101 le 8 Avr 2011
Thanks Sean de, I just had a total blindspot there for some reason!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 8 Avr 2011
0<x<1 does not do what you want it to!! It is parsed as
(0<x)<1 . 0<x returns a boolean value, 0 or 1, and then that value is tested according to whether it is < 1 which is only the case if the operation was false. The code is thus equivalent to
~(0<x)
and so is the same as
isnan(x) || 0 >= x
I doubt that you would want 0<x<1 to be true when x is nan !!

Catégories

En savoir plus sur Cell Arrays 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!

Translated by