Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
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. Several matrix-generating functions are listed below:

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: PRINT statement.
   reset print;


The BLOCK Function

The BLOCK function has the general form

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

and creates a block-diagonal matrix from the argument matrices. For example, the statements
   >  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


The J Function

The J function has the general form

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

and creates a matrix having nrow rows, ncol columns, and all element values equal to value. The ncol and value arguments are optional, but you will usually want to 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 ×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 general form

I( dimension );

where dimension gives the number of rows. For example, the following statement creates a 3 ×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 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 n1=3, n2=2, and n3=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 general form

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

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
Chapter Contents
Previous
Previous
Next
Next
Top
Top

Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.