16.2 Comparison Operators


x == y
elementwise equality of x and y
x !=y or x <> y
elementwise inequality of x and y
x > y
checks if x is elementwise greater than y
x >= y
checks if x is elementwise greater than or equal to y
x < y
checks if x is elementwise less than y
x <= y
checks if x is elementwise less than or equal to y
x && y
elementwise logical AND operator for x and y
x ||y
elementwise logical OR operator for x and y
!x
elementwise logical NOT operator for x

XploRe has no specific data type for boolean values. The boolean value true is encoded by 1, whereas the value false is encoded by 0. Hence, all matrix operations for numeric matrices can be used for boolean expressions as well. Note that many functions which require boolean input only check if the input is false or not.

Let us consider which can be found in 36670 XLGmatrix03.xpl .

  A = (2|4)~(8|8)
  B = (5|6)~(4|8)
  A > B
The last instruction, i.e. the comparison yields
  Contents of _tmp
  [1,]        0        1 
  [2,]        0        0
which means that only the element in the first row and second column of A is greater than the corresponding element of B. Similarly,
  A == B
gives
  Contents of _tmp
  [1,]        0        0 
  [2,]        0        1
Note that A == B is different from the assignment A = B, which would create A as a copy of the matrix B.

You may check now that the instruction

  (A > B) || (A == B)
which checks if ''A > B or A == B'' is identical to
  A >= B
Both instructions return
  Contents of _tmp
  [1,]        0        1 
  [2,]        0        1
which means that both values in the second column fulfill the >= condition.

Let us point out again that all introduced comparison operators work elementwise. They extend to arrays as usual. To verify a condition for all elements of a matrix or an array, the 36673 prod function can be used:

  prod(prod(A==B),2)
would return 1 in the case that all elements of the matrices A and B are identical. We will learn more about 36676 prod in Section 16.4.