Logical Operators:   &,   |,   ^

matrix1 & matrix2 ;

matrix & scalar ;

matrix & vector ;

matrix1 | matrix2 ;

matrix | scalar ;

matrix | vector ;

^matrix ;

The logical operators compare two matrices element by element and create a new matrix. For logical comparisons, a missing value is handled as if it is a zero value. That is, in the text that follows in this section, nonzero really means nonzero and nonmissing.

An element of the new matrix computed by the OR operator (|) is 1 if either of the corresponding elements of matrix1 and matrix2 is nonzero. If both are zero (or missing), the new element is zero.

An element of the new matrix computed by the AND logical operator (&) is 1 if the corresponding elements of matrix1 and matrix2 are both nonzero; otherwise, it is zero.

If either operand is a scalar, the OR and AND operators perform a logical comparison between each element and the scalar value. If either operand is a row or column vector, then the operation is performed by using that vector on each of the rows or columns of the matrix.

The NOT prefix operator (^) examines each element of a matrix and computes a new matrix that contains elements that are ones and zeros. If an element of matrix is zero or missing, the corresponding element in the new matrix is 1. If an element of matrix is nonzero, the corresponding element in the new matrix is 0.

The following statements illustrate the use of these logical operators. The results are shown in Figure 24.18:

x = {0 1 0 1 . .};
y = {1 1 0 0 1 0};
u = x|y;
v = x&y;
w = ^x;
print u, v, w;

Figure 24.18: Results of Logical Comparisons

u
1 1 0 1 1 0

v
0 1 0 0 0 0

w
1 0 1 0 1 1