What is the difference between if and if/and in MATLAB?
Mostrar comentarios más antiguos
Could you please explain if and in MATLAB with an example?
Are these two the same?
if (y1<8)&&(y1>-8)
p1=0
end
if and(y1<8,y1>-8)
p1=0
end
Respuestas (2)
Jan
el 12 de Nov. de 2011
1 voto
Both commands are very similar:
- and() evaluates both arguments and accepts arrays.
- && omits the evaluation of the 2nd argument, if the first is false already. It works on scalar inputs only.
Fangjun Jiang
el 12 de Nov. de 2011
If you type in "help and", it will tell you that and(A,B) is the same as A & B most of the times. From the help, "C = AND(A,B) is called for the syntax 'A & B' when A or B is an object.".
&& is called "Short-circuit logical AND", it applies only to scalar. Try this to understand it.
A=logical([1 0 1]);
B=logical([1 1 0]);
C=A & B;
D=and(A,B)
a=true;
b=false;
c=a && b;
d=A && B
Categorías
Más información sobre Scope Variables and Generate Names 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!