Logical Operations

逻辑运算指逻辑运算符逻辑运算函数作用于逻辑值的运算过程

!! 这里逻辑值为广义逻辑值, 即任意非 0 数值和非 char(0) 字符视为逻辑真 1 (即 x 视为 logical(x))

逐元素逻辑运算

OperatorEquivalent Function
&and
~not
  • 以上与运算 &或运算 | 都是 binary operation, 运算 arrays 需遵循 Matlab Compatible Array Sizes
  • 逻辑非 ~ 运算为单目运算

其他逐元素逻辑运算函数

  • 亦或运算 xor
  • & 可扩展为函数 all
  • | 可扩展为函数 any, 用法同 and

捷径逻辑运算

OperatorDescription
&&& 的惰性运算
  • 捷径 short-circuit 逻辑运算符的存在意味着之前逻辑运算都是要完整运算两个操作对象
  • 一般都使用捷径逻辑运算符, 特别当运算中第二个表达式依赖于第一个条件成立时, 则只能使用捷径逻辑运算符
    • 例如

      >> a = "a";
      >> isnumeric(a) && (1/a > 3)
      ans =
        logical
         0
      >> (a ~= 0) & (1/a > 3) 
      Error using /
      Arguments must be numeric, char, or logical.