Logical Operations
A logical operation applies a logical operator or logical function to logical values.
- Here a “logical value” is the generalized one—any non-zero numeric value or any non-
char(0)character is treated as logical true (1); that is, x is treated aslogical(x).
Element-Wise Logical Operations
| Operator | Equivalent Function |
|---|---|
& | and |
| |
~ | not |
- The AND
&and OR|operators are binary; their array operands must satisfy Matlab Compatible Array Sizes - The NOT
~operator is unary
Other Element-Wise Logical Functions
Short-Circuit Logical Operations
| Operator | Description |
|---|---|
&& | Lazy version of & |
|
- The existence of short-circuit operators implies that the element-wise logical operations always evaluate both operands in full
- Prefer short-circuit operators in general; they are required whenever the second expression depends on the first being true
-
For example:
>> a = "a"; >> isnumeric(a) && (1/a > 3) ans = logical 0 >> (a ~= 0) & (1/a > 3) Error using / Arguments must be numeric, char, or logical.
-