Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
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
# elementwise multiplication
## elementwise power
/ division
<> element maximum
>< element minimum
| logical OR
& logical AND
< less than
<= less than or equal to
> greater than
>= 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 given below.

{Let } A = [ 2 & 2 \ 3 & 4 \ ]
{ and } B = [ 4 & 5 \ 1 & 0 \ ]
The addition operator (+) adds corresponding matrix elements:
A + B { yields }
[ 6 & 7 \ 4 & 4 \ ]
The elementwise multiplication operator (#) multiplies corresponding elements:
A \char93  B { yields }
[ 8 & 10 \ 3 & 0 \ ]
The elementwise power operator (##) raises elements to powers:
A \char93 \char93 2 { yields }
[ 4 & 4 \ 9 & 16 \ ]
The element maximum operator (<>) compares corresponding elements and chooses the larger:
A \lt\gt B { yields }
[ 4 & 5 \ 3 & 4 \ ]
The less than or equal to operator (<=) 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:
{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 statement

   x=x#(x>0);
replaces each negative element of the matrix X with 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: PRINTALL command to have the temporary result matrices displayed.

Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
Top
Top

Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.