Matrix Relational Operations

The way matrices can be used within if-statement tests is special. The result of a matrix relational test, such as A == B, is a matrix the same size as A and B filled with ones and zeros according to the result of an element-by-element test. If either of the operands is scalar, or a 1-by-1 matrix, then the element-by-element test is performed as before, by using the scalar value repeatedly. For example.

> a = [1, 2, 3; 4, 5, 6; 7, 8, 9];
> b = a';
> a == b
        1          0          0  
        0          1          0  
        0          0          1  
> a >= 5
        0          0          0  
        0          1          1  
        1          1          1

RLaB  if-tests do not accept matrices. The built-in functions any() and all() can be used in combination with relational and logical tests to conditionally execute statements based upon matrix properties. For example: perform a test that returns true or false (0 or 1) if a contains the value 4.

> any ( any (a == 4))

The function any() returns true if any of the element(s) of its argument are non-zero. The function all() returns true if all of the element(s) of its argument are non-zero. Note that any is used twice; this is because any is a vector-oriented function. This will be discussed later.