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 as logical(x).

Element-Wise Logical Operations

OperatorEquivalent Function
&and
~not

Other Element-Wise Logical Functions

  • XOR: xor
  • & extends to the function all
  • | extends to the function any, with the same usage as all

Short-Circuit Logical Operations

OperatorDescription
&&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.