A&B
(1)首先判断A的逻辑值,然后判断B的值,然后进行逻辑与的计算。
(2)A和B可以为矩阵(e.g. A=[1 0],B=[0 0])。
A&&B
(1)首先判断A的逻辑值,如果A的值为假,就可以判断整个表达式的值为假,就不需要再判断B的值。
(2)A和B不能是矩阵,只能是标量。
"|"与“||”同理。
以下是Matlab帮助文档中的描述:
& Element-wise Logical AND.
A & B is a matrix whose elements are logical 1 (TRUE) where both A
and B have non-zero elements, and logical 0 (FALSE) where either has
a zero element. A and B must have the same dimensions (or one can
be a scalar).
&& Short-Circuit Logical AND.
A && B is a scalar value that is the logical AND of scalar A and B.
This is a "short-circuit" operation in that MATLAB evaluates B only
if the result is not fully determined by A. For example, if A equals
0, then the entire expression evaluates to logical 0 (FALSE), regard-
less of the value of B. Under these circumstances, there is no need
to evaluate B because the result is already known.
| Element-wise Logical OR.
A | B is a matrix whose elements are logical 1 (TRUE) where either
A or B has a non-zero element, and logical 0 (FALSE) where both have
zero elements. A and B must have the same dimensions (or one can
be a scalar).
|| Short-Circuit Logical OR.
A || B is a scalar value that is the logical OR of scalar A and B.
This is a "short-circuit" operation in that MATLAB evaluates B only
if the result is not fully determined by A. For example, if A equals
1, then the entire expression evaluates to logical 1 (TRUE), regard-
less of the value of B. Under these circumstances, there is no need
to evaluate B because the result is already known.