8.5.1 Element-by-element Boolean Operators

An element-by-element boolean expression is a combination of comparison expressions using the boolean operators “or” (‘|’), “and” (‘&’), and “not” (‘!’), along with parentheses to control nesting. The truth of the boolean expression is computed by combining the truth values of the corresponding elements of the component expressions. A value is considered to be false if it is zero, and true otherwise.

Element-by-element boolean expressions can be used wherever comparison expressions can be used. They can be used in if and while statements. However, a matrix value used as the condition in an if or while statement is only true if all of its elements are nonzero.

Like comparison operations, each element of an element-by-element boolean expression also has a numeric value (1 if true, 0 if false) that comes into play if the result of the boolean expression is stored in a variable, or used in arithmetic.

Here are descriptions of the three element-by-element boolean operators.

boolean1 & boolean2

Elements of the result are true if both corresponding elements of boolean1 and boolean2 are true.

boolean1 | boolean2

Elements of the result are true if either of the corresponding elements of boolean1 or boolean2 is true.

! boolean
~ boolean

Each element of the result is true if the corresponding element of boolean is false.

These operators work on an element-by-element basis. For example, the expression

[1, 0; 0, 1] & [1, 0; 2, 3]

returns a two by two identity matrix.

For the binary operators, broadcasting rules apply. See Broadcasting. In particular, if one of the operands is a scalar and the other a matrix, the operator is applied to the scalar and each element of the matrix.

For the binary element-by-element boolean operators, both subexpressions boolean1 and boolean2 are evaluated before computing the result. This can make a difference when the expressions have side effects. For example, in the expression

a & b++

the value of the variable b is incremented even if the variable a is zero.

This behavior is necessary for the boolean operators to work as described for matrix-valued operands.

: z = and (x, y)
: z = and (x1, x2, …)

Return the logical AND of x and y.

This function is equivalent to the operator syntax x & y. If more than two arguments are given, the logical AND is applied cumulatively from left to right:

(…((x1 & x2) & x3) & …)

See also: or, not, xor.

: z = not (x)

Return the logical NOT of x.

This function is equivalent to the operator syntax x.

See also: and, or, xor.

: z = or (x, y)
: z = or (x1, x2, …)

Return the logical OR of x and y.

This function is equivalent to the operator syntax x | y. If more than two arguments are given, the logical OR is applied cumulatively from left to right:

(…((x1 | x2) | x3) | …)

See also: and, not, xor.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Element_002dby_002delement-Boolean-Operators.html