|
Chapter Contents |
Previous |
Next |
| Working with Matrices |
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 \ ]](images/im4eq5.gif)
![A + B { yields }
[ 6 & 7 \ 4 & 4 \ ]](images/im4eq6.gif)
![A \char93 B { yields }
[ 8 & 10 \ 3 & 0 \ ]](images/im4eq7.gif)
![A \char93 \char93 2 { yields }
[ 4 & 4 \ 9 & 16 \ ]](images/im4eq8.gif)
![A \lt\gt B { yields }
[ 4 & 5 \ 3 & 4 \ ]](images/im4eq9.gif)
![A \lt= B { yields }
[ 1 & 1 \ 0 & 0 \ ]](images/im4eq10.gif)
![{MOD}(A,3) { yields }
[ 2 & 2 \ 0 & 1 \ ]](images/im4eq11.gif)
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 |
Previous |
Next |
Top |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.