Working with Matrices

Matrix-Generating Functions

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. Here are several matrix-generating functions:

BLOCK
creates a block-diagonal matrix.

DESIGNF
creates a full-rank design matrix.

I
creates an identity matrix.

J
creates a matrix of a given dimension.
SHAPE
shapes a new matrix from the argument.

The sections that follow illustrate these matrix-generating functions. Again, they are shown with automatic printing of results, activated by invoking the RESET statement with the PRINT option.

  
    reset print;
 

The BLOCK Function

The BLOCK function has the following general form:

BLOCK( matrix1,<matrix2,...,matrix15 >);

and creates a block-diagonal matrix from the argument matrices. For example, the following statements form a block-diagonal matrix:
  
    >  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);
 
Here is the resulting matrix:
  
             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

The J function has the following general form:

J( nrow<,ncol<,value> > );

It creates a matrix having nrow rows, ncol columns, and all element values equal to value. The ncol and value arguments are optional, but you should usually specify them. In many statistical applications, it is helpful to be able to create a row (or column) vector of 1s (you did so to calculate coffee totals in the last section). You can do this with the J function. For example, the following statement creates a 1 x 5 row vector of 1s:

  
    >  one=j(1,5,1); 
  
              ONE           1 row       5 cols    (numeric) 
                      1         1         1         1         1
 


The I Function

The I function creates an identity matrix of a given size. It has the following general form:

I( dimension );

where dimension gives the number of rows. For example, the following statement creates a 3 x 3 identity matrix:
  
    >  I3=I(3); 
  
                 I3            3 rows      3 cols    (numeric) 
  
                             1        0          0 
                             0        1          0 
                             0        0          1
 


The DESIGNF Function

The DESIGNF function generates a full-rank design matrix, useful in calculating ANOVA tables. It has the following general form:

DESIGNF( column-vector );

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 n_1=3, n_2=2, and n_3=2 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 SHAPE Function

The SHAPE function shapes a new matrix from an argument matrix. It has the following general form:

SHAPE( matrix<,nrow<,ncol<,pad-value >);

Although the nrow, ncol, and pad-value arguments are optional, you should usually specify them. The following statement uses the SHAPE function to create a 3 x 3 matrix containing 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,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 the matrix with 0s after the first cycle through the argument matrix.

Previous Page | Next Page | Top of Page