What is the difference between if and if/and in MATLAB?
Afficher commentaires plus anciens
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
Réponses (2)
Jan
le 12 Nov 2011
1 vote
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
le 12 Nov 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
Catégories
En savoir plus sur Scope Variables and Generate Names dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!