Chapter Contents Previous Next
 Working with Matrices

## Subscripts

Subscripts are special postfix operators placed in square brackets ([ ]) after a matrix operand. Subscript operations have the general form

operand[row,column]
where
operand
is usually a matrix name, but it can also be an expression or literal.

row
refers to an expression, either scalar or vector, for selecting one or more rows from the operand.
column
refers to an expression, either scalar or vector, for selecting one or more columns from the operand.

You can use subscripts to

• refer to a single element of a matrix
• refer to an entire row or column of a matrix
• refer to any submatrix contained within a matrix
• perform a reduction across rows or columns of a matrix
In expressions, subscripts have the same (high) precedence as the transpose postfix operator (). Note that when both row and column subscripts are used, they are separated by a comma.

### Selecting a Single Element

You can select a single element of a matrix in two ways. You can use two subscripts (row, column) to refer to its location, or you can use one subscript to look for the element down the rows. For example, referring to the coffee example used earlier, to find the element corresponding to the number of cups that Linda drank on Monday, you can use either of two statements.

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
```

### Selecting a Row or Column

To refer to an entire row or column of a matrix, write the subscript with the row or column number, omitting the other subscript but not the comma. For example, to refer to the row of COFFEE that corresponds to Jim, you want the submatrix consisting of the third row and all columns:
```   >  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
```

### Submatrices

You refer to a submatrix by the specific rows and columns you want. Include within the brackets the rows you want, a comma, and the columns you want. For example, to create the submatrix of COFFEE consisting of the first and third rows and the second, third, and fifth columns, submit the following statements:
```   >  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.

### Subscripted Assignment

You can assign values into a matrix using subscripts to refer to the element or submatrix. In this type of assignment, the subscripts appear on the left-hand side of the equal sign. For example, to change the value in the first row, second column of COFFEE from 2 to 4, use subscripts to refer to the appropriate element in an assignment statement:
```   >  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