Next: , Up: Boolean Expressions   [Contents][Index]


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.

: TF = and (x, y)
: TF = 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.

: TF = or (x, y)
: TF = 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.


Next: Short-circuit Boolean Operators, Up: Boolean Expressions   [Contents][Index]