Working with Matrices


Subscript Reduction Operators

A reduction operator is a statistical operation (for example, a sum or a mean) that returns a matrix of a smaller dimension. Reduction operators are often encountered in frequency tables: the marginal frequencies represent the sum of the frequencies across rows or down columns.

In SAS/IML software, you can use reduction operators in place of values for subscripts to get reductions across all rows or columns. Table 5.3 lists operators for subscript reduction.

Table 5.3: Subscript Reduction Operators

Operator

Description

$+$

Addition

$\# $

Multiplication

$<>$

Maximum

$>$$<$

Minimum

$<:>$

Index of maximum

$>:<$

Index of minimum

$:$

Mean

$\# \# $

Sum of squares


For example, to get row sums of a matrix X, you can sum across the columns with the syntax X[,+]. Omitting the first subscript specifies that the operator apply to all rows. The second subscript (+) specifies that summation reduction take place across the columns. The elements in each row are added, and the new matrix consists of one column that contains the row sums.

To give a specific example, consider the coffee data from earlier in the chapter. The following statements use the summation reduction operator to compute the sums for each row:

coffee={4 2 2 3 2, 3 3 1 2 1, 2 1 0 2 1, 5 4 4 3 4};
names={Jenny, Linda, Jim, Samuel};
mattrib coffee rowname=names colname={'MON' 'TUE' 'WED' 'THU' 'FRI'};
Total = coffee[,+];
print coffee Total;

Figure 5.30: Summation across Columns to Find the Row Sums

coffee MON TUE WED THU FRI Total
JENNY 4 2 2 3 2 13
LINDA 3 3 1 2 1 10
JIM 2 1 0 2 1 6
SAMUEL 5 4 4 3 4 20



You can use these reduction operators to reduce the dimensions of rows, columns, or both. When both rows and columns are reduced, row reduction is done first.

For example, the expression $\mb{A}[+,<>]$ results in the maximum $(<>)$ of the column sums $(+)$.

You can repeat reduction operators. To get the sum of the row maxima, use the expression $\mb{A}[,<>] [+,]$, or, equivalently, $\mb{A}[,<>][+]$.

A subscript such as $\mb{A}[\{ 2\;  3\} ,+]$ first selects the second and third rows of $\mb{A}$ and then finds the row sums of that submatrix.

The following examples demonstrate how to use the operators for subscript reduction. Consider the following matrix:

\[  \mb{A} = \left[ \begin{array}{rrr} 0 &  1 &  2 \\ 5 &  4 &  3 \\ 7 &  6 &  8 \\ \end{array} \right]  \]

The following statements are true:

\[  \mb{A}[\{ 2\;  3\} ,+] \mbox{ is } \left[ \begin{array}{r} 12 \\ 21 \\ \end{array} \right] \mbox{ (row sums for rows 2 and 3)}  \]
\[  \mb{A}[+,<>] \mbox{ is } \left[ \begin{array}{r} 13 \\ \end{array} \right] \mbox{ (maximum of column sums)}  \]
\[  \mb{A}[<>,+] \mbox{ is } \left[ \begin{array}{r} 21 \\ \end{array} \right] \mbox{ (sum of column maxima)}  \]
\[  \mb{A}[,><] [+,] \mbox{ is } \left[ \begin{array}{r} 9 \\ \end{array} \right] \mbox{ (sum of row minima)}  \]
\[  \mb{A}[,<:>] \mbox{ is } \left[ \begin{array}{r} 3 \\ 1 \\ 3 \\ \end{array} \right] \mbox{ (indices of row maxima)}  \]
\[  \mb{A}[>:<,] \mbox{ is } \left[ \begin{array}{rrr} 1 &  1 &  1 \\ \end{array} \right] \mbox{ (indices of column minima)}  \]
\[  \mb{A}[:] \mbox{ is } \left[ \begin{array}{r} 4 \\ \end{array} \right] \mbox{ (mean of all elements)}  \]