What is the difference between if and if/and in MATLAB?

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

답변 (2개)

Jan
Jan 2011년 11월 12일

1 개 추천

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
Fangjun Jiang 2011년 11월 12일
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

카테고리

도움말 센터File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

질문:

2011년 11월 12일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by