writing "if" command for a>f>c
Mostrar comentarios más antiguos
how can write if command for < and > together. for example if 1.01>a>0.9 then do cprintf('g','ok') else do cprintf('r','not ok'). when i write this like this: if 1.01>a>0.9 cprintf('g','ok'); else cprintf('r','not ok');
matlab only considers 1.01>a
thanks alot
Respuesta aceptada
Más respuestas (1)
geomod
el 30 de Ag. de 2011
2 votos
I cannot say why but, if you write:
if 0.9<a<1.01
OR
if 1.01>a && a>0.9
then it should run...
3 comentarios
Paulo Silva
el 30 de Ag. de 2011
you are right about the syntax of the if operation, +1 vote
mohammad
el 30 de Ag. de 2011
Walter Roberson
el 31 de Ag. de 2011
if 0.9<a<1.01
means
if (0.9<a)<1.01
The part in () will evaluate to a logical value, either false (numeric 0) or true (numeric 1). Both 0 and 1 are < 1.01 so the overall expression would always evaluate as true.
MATLAB has no three-part logical operators at all, no built in way to test ranges.
If you wanted to really mess with the mind of the person reading the code, you could use:
if ~isempty(0.9:a-0.9:1.01)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!