CALL SORTNDX (index, matrix <, by> <, descend> );
The SORTNDX subroutine creates an index to reorder a matrix by specified columns.
The SORTNDX subroutine returns the following value:
is a vector such that index[i] is the row index of the ith element of matrix when sorted according to by and descend. Consequently, matrix[index, ] is the sorted matrix.
The arguments to the SORTNDX subroutine are as follows:
is the input matrix, which is not modified by the call.
specifies the columns used to sort the matrix. The argument by is either a numeric matrix that contains column numbers, or a character matrix that contains the names of columns assigned to matrix by a MATTRIB statement or READ statement . If by is not specified, then the first column is used.
specifies which columns, if any, should be sorted in descending order. Any by columns not specified as descending will be ascending. If descend = by, then all by columns will be descending; if descend is skipped or is a null matrix, then all by columns will be ascending.
The SORTNDX subroutine can be used to process the rows of a matrix in a sorted order, without having to actually modify the matrix.
For example, the following statements return a vector that specifies the order of the rows in a matrix:
m = { 1 1 0, 2 0 0, 1 3 1, 2 2 2 }; call sortndx(ndx, m, {1 3}, 3); print ndx;
The output is shown in Figure 25.382. The SORTNDX subroutine returns the vector ndx
that indicates how rows of m
will appear if you sort m
in ascending order by column 1 and in descending order by column 3. The values of the vector ndx
indicate that row 3 of m
will be the first row in the sorted matrix. Row 1 of m
will become the second row. Row 4 will become the third row, and row 2 will become the last row.
The matrix can be physically sorted as follows:
sorted = m[ndx,];
The SORTNDX subroutine can be used with the UNIQUEBY function to extract the unique combinations of values in the by columns. If you want to replace a matrix with a sorted copy of itself, use the SORT call .