|
按照help中的解释,区别是- Note the difference between the elementwise and short-circuit logical operators. Short-circuit operators, such as && and ||, test only as much of the input expression as necessary. In the second part of this example, it makes no difference that B is undefined because the state of A alone determines that the expression is false:
- A = 0;
- A & B
- ??? Undefined function or variable 'B'.
- A && B
- ans =
- 0
复制代码 当然,还有
- & 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.
复制代码 当然在实际运用过程中,需要达到自己的效果是,如果用&不行,就用&&,反正也不多,试一试呗 |
|