matrix[rows, columns] ;
matrix[elements] ;
Subscripts are used with matrices to select submatrices, where rows and columns are expressions that evaluate to scalars or vectors. If these expressions are numeric, they must contain valid subscript values of rows and columns in the argument matrix.
For example, the following statements select elements from the second row of the matrix x
:
x = {1 2 3, 4 5 6, 7 8 9}; a = 3; y = x[2, a]; b = 1:3; z = x[2, b]; w = x[{4 6}]; print y, z, w;
The output is shown in Figure 24.27. The matrix y
contains the element of x
from the second row and the third column. The matrix z
contains the entire second row of x
. The matrix w
contains the fourth and sixth elements of x
. Because SAS/IML software store matrices in row-major order, w
contains the first and third elements from the second row of x
.
If a row or column expression is a character matrix, then it refers to columns or rows in the argument matrix that are assigned
corresponding labels by a MATTRIB statement or READ statement. For example, the following statements select elements from the second row of x
, and from the first and third columns:
x = {1 2 3, 4 5 6, 7 8 9}; c = "col1":"col3"; r = "row1":"row3"; mattrib x colname=c rowname=r; a = {"col1" "col3"}; m = x["row2", a]; print m;
A subscripted matrix can appear on the left side of the equal sign. The dimensions of the target submatrix must conform to the dimensions of the source matrix, as shown in the following statements:
x[1, {1 3}] = .; x[{1 2}, 2] = {0, 1}; x[7] = -1; print x;
Figure 24.29: Result of Assigning Submatrices of an Existing Matrix
x | |||
---|---|---|---|
col1 | col2 | col3 | |
row1 | . | 0 | . |
row2 | 4 | 1 | 6 |
row3 | -1 | 8 | 9 |
See the section Using Matrix Expressions for further information about matrix subscripts.