|
Chapter Contents |
Previous |
Next |
| Working with Matrices |
Subscripts are special postfix operators placed in square brackets ([ ]) after a matrix operand. Subscript operations have the general form
You can use subscripts to
First, you can refer to the element by row and column location. In this case, you want the second row and first column. You can call this matrix c21.
> print coffee[rowname=names];
COFFEE
JENNY 4 2 2 3 2
LINDA 3 3 1 2 1
JIM 2 1 0 2 1
SAMUEL 5 4 4 3 4
> c21=coffee[2,1];
> print c21;
C21
3
You can also look for the element down the rows.
In this case, you refer to this element as the
sixth element of COFFEE in row-major order.
> c6=coffee[6];
> print c6;
C6
3
> jim=coffee[3,];
> print jim;
JIM
2 1 0 2 1
If you want the data for Friday, you know that the
fifth column corresponds to Friday, so you want the
submatrix consisting of the fifth column and all rows:
> friday=coffee[,5];
> print friday;
FRIDAY
2
1
1
4
> submat1=coffee[{1 3},{2 3 5}];
> print submat1;
SUBMAT1
2 2 2
1 0 1
The first vector, {1 3}, selects the rows, and
the second vector, {2 3 5}, selects the columns.
Alternately, you can create the vectors
beforehand and supply their names as arguments.
> rows={1 3};
> cols={2 3 5};
> submat1=coffee[rows,cols];
You can use index vectors generated by the index creation operator (:) in subscripts to refer to successive rows or columns. For example, to select the first three rows and last three columns of COFFEE, use the following statements:
> submat2=coffee[1:3,3:5];
> print submat2;
SUBMAT2
2 3 2
1 2 1
0 2 1
Note that, in each example, the number in the first subscript defines the number of rows in the new matrix; the number in the second subscript defines the number of columns.
> coffee[1,2]=4;
> print coffee;
COFFEE
4 4 2 3 2
3 3 1 2 1
2 1 0 2 1
5 4 4 3 4
To change the values in the last column of
COFFEE to 0s use the following statement:
> coffee[,5]={0,0,0,0};
> print coffee;
COFFEE
4 4 2 3 0
3 3 1 2 0
2 1 0 2 0
5 4 4 3 0
In the next example, you first locate the positions of negative elements of a matrix and then set these elements equal to 0. This can be useful in situations where negative elements may indicate errors or be impossible values. The LOC function is useful for creating an index vector for a matrix that satisfies some condition. In the following example, the LOC function is used to find the positions of the negative elements of the matrix T and then to set these elements equal to 0 using subscripted assignment:
> t={ 3 2 -1,
> 6 -4 3,
> 2 2 2 };
> print t;
T
3 2 -1
6 -4 3
2 2 2
> i=loc(t<0);
> print i;
I
3 5
> t[i]=0;
> print t;
T
3 2 0
6 0 3
2 2 2
Subscripts can also contain expressions with results that are either row or column vectors. These statements can also be written
> t[loc(t<0)]=0;
If you use a noninteger value as a subscript, only the integer portion is used. Using a subscript value less than one or greater than the dimension of the matrix results in an error.
|
Chapter Contents |
Previous |
Next |
Top |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.