Working with Matrices

Elementwise Binary Operators

Elementwise binary operators produce a result matrix from element-by-element operations on two argument matrices.

Table 4.2 lists the elementwise binary operators.

Table 4.2: Elementwise Binary Operators
Operator   Action
+ addition, concatenation
- subtraction
\char93  elementwise multiplication
\char93 \char93  elementwise power
/ division
> element maximum
< element minimum
| logical OR
\& logical AND
< less than
\lt= less than or equal to
> greater than
\gt= greater than or equal to
^= not equal to
= equal to
MOD(m,n) modulo (remainder)

For example, consider the following two matrices a and b:

{let } a = [ 2 & 2 \ 3 & 4 \    ]   { and } b = [ 4 & 5 \ 1 & 0 \    ]

The addition operator (+) adds corresponding matrix elements, as follows:

a + b { yields }   [ 6 & 7 \ 4 & 4 \    ]

The elementwise multiplication operator (\char93 ) multiplies corresponding elements, as follows:

a \char93  b { yields }   [ 8 & 10 \ 3 & 0 \    ]

The elementwise power operator (\char93 \char93 ) raises elements to powers, as follows:

a \char93 \char93 2 { yields }   [ 4 & 4 \ 9 & 16 \    ]

The element maximum operator (\lt\gt) compares corresponding elements and chooses the larger, as follows:

a \lt\gt b { yields }   [ 4 & 5 \ 3 & 4 \    ]
The less than or equal to operator (\lt=) returns a 1 if an element of a is less than or equal to the corresponding element of b, and returns a 0 otherwise:
a \lt= b { yields }   [ 1 & 1 \ 0 & 0 \    ]

The modulo operator returns the remainder of each element divided by the argument, as follows:

{mod}(a,3) { yields }   [ 2 & 2 \ 0 & 1 \    ]

All operators can also work in a one-to-many or many-to-one manner, as well as in an element-to-element manner; that is, they enable you to perform tasks such as adding a scalar to a matrix or dividing a matrix by a scalar. For example, the following statement replaces each negative element of the matrix x with 0:

  
    x=x#(x>0);
 
The expression (X>0) is a many-to-one operation that compares each element of x to 0 and creates a temporary matrix of results; an element in the result matrix is 1 when the expression is true and 0 when it is false. When the expression is true (the element is positive), the element is multiplied by 1. When the expression is false (the element is negative or 0), the element is multiplied by 0. To fully understand the intermediate calculations, you can use the RESET statement with the PRINTALL option to have the temporary result matrices displayed.

Previous Page | Next Page | Top of Page