Functions That Generate Matrices |
SAS/IML software has many useful built-in functions that generate matrices. For example, the J function creates a matrix with a given dimension and specified element value. You can use this function to initialize a matrix to a predetermined size. Here are several functions that generate matrices:
creates a block-diagonal matrix
creates a full-rank design matrix
creates an identity matrix
creates a matrix of a given dimension
creates a new matrix by repeating elements of the argument matrix
shapes a new matrix from the argument
The sections that follow illustrate the functions that generate matrices. The output of each example is generated automatically by using the RESET PRINT statement:
reset print;
The BLOCK function has the following general form:
The BLOCK function creates a block-diagonal matrix from the argument matrices. For example, the following statements form a block-diagonal matrix:
a = {1 1, 1 1}; b = {2 2, 2 2}; c = block(a,b);
c 4 rows 4 cols (numeric) |
1 | 1 | 0 | 0 |
1 | 1 | 0 | 0 |
0 | 0 | 2 | 2 |
0 | 0 | 2 | 2 |
The J function has the following general form:
It creates a matrix that has nrow rows, ncol columns, and all elements equal to value. The ncol and value arguments are optional; if they are not specified, default values are used. In many statistical applications, it is helpful to be able to create a row (or column) vector of ones. (You did so to calculate coffee totals in the previous section.) You can do this with the J function. For example, the following statement creates a column vector of ones:
ones = j(5,1,1);
ones 5 rows 1 col (numeric) |
1 |
1 |
1 |
1 |
1 |
The I function creates an identity matrix of a given size. It has the following general form:
where dimension gives the number of rows. For example, the following statement creates a identity matrix:
I3 = I(3);
I3 3 rows 3 cols (numeric) |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 1 |
The DESIGNF function generates a full-rank design matrix, which is useful in calculating ANOVA tables. It has the following general form:
For example, the following statement creates a full-rank design matrix for a one-way ANOVA, where the treatment factor has three levels and there are , , and observations at the factor levels:
d = designf({1,1,1,2,2,3,3});
d 7 rows 2 cols (numeric) |
1 | 0 |
1 | 0 |
1 | 0 |
0 | 1 |
0 | 1 |
-1 | -1 |
-1 | -1 |
The REPEAT function creates a new matrix by repeating elements of the argument matrix. It has the following syntax:
The function repeats matrix a total of nrowncol times. The argument is repeated nrow times in the vertical direction and ncol times in the horizontal direction. For example, the following statement creates a matrix:
x = {1 2, 3 4}; r = repeat(x, 2, 3);
r 4 rows 6 cols (numeric) |
1 | 2 | 1 | 2 | 1 | 2 |
3 | 4 | 3 | 4 | 3 | 4 |
1 | 2 | 1 | 2 | 1 | 2 |
3 | 4 | 3 | 4 | 3 | 4 |
The SHAPE function creates a new matrix by reshaping an argument matrix. It has the following general form:
The ncol and pad-value arguments are optional; if they are not specified, default values are used. The following statement uses the SHAPE function to create a matrix that contains the values 99 and 33. The function cycles back and repeats values to fill in the matrix when no pad-value is given.
aa = shape({99 33, 33 99}, 3, 3);
aa 3 rows 3 cols (numeric) |
99 | 33 | 33 |
99 | 99 | 33 |
33 | 99 | 99 |
Alternatively, you can specify a value for pad-value that is used for filling in the matrix:
bb = shape({99 33, 33 99}, 3, 3, 0);
bb 3 rows 3 cols (numeric) |
99 | 33 | 33 |
99 | 0 | 0 |
0 | 0 | 0 |
The SHAPE function cycles through the argument matrix elements in row-major order and fills in the matrix with zeros after the first cycle through the argument matrix.