|
Chapter Contents |
Previous |
Next |
| Working with Matrices |
SAS/IML software has many built-in functions that generate useful matrices. For example, the J function creates a matrix with a given dimension and element value when you supply the number of rows and columns, and an element value for the new matrix. This function is useful to initialize a matrix to a predetermined size. Several matrix-generating functions are listed below:
reset print;
> a={1 1,1 1};
A 2 rows 2 cols (numeric)
1 1
1 1
> b={2 2, 2 2};
B 2 rows 2 cols (numeric)
2 2
2 2
> c=block(a,b);
result in the matrix
C 4 rows 4 cols (numeric)
1 1 0 0
1 1 0 0
0 0 2 2
0 0 2 2
> one=j(1,5,1);
ONE 1 row 5 cols (numeric)
1 1 1 1 1
> I3=I(3);
I3 3 rows 3 cols (numeric)
1 0 0
0 1 0
0 0 1
> 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
Although the nrow, ncol, and pad-value arguments are optional, you will usually want to specify them. The following example uses the SHAPE function to create a 3 ×3 matrix containing the values 99 and 33. The function cycles back and repeats values to fill in when no pad-value is given.
> aa=shape({99 33,99 33},3,3);
AA 3 rows 3 cols (numeric)
99 33 99
33 99 33
99 33 99
In the next example, a pad-value
is specified for filling in the matrix:
> aa=shape({99 33,99 33},3,3,0);
AA 3 rows 3 cols (numeric)
99 33 99
33 0 0
0 0 0
The SHAPE function cycles through the argument matrix
elements in row-major order and then fills in with
0s after the first cycle through the argument matrix.
|
Chapter Contents |
Previous |
Next |
Top |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.