Sorting, Subsetting, and Concatenation

MSort() sorts the rows of a matrix according to one of the columns of the matrix. The column elements are sorted by a heap sort, then the rows of the new matrix are stored in the order of the sorted column elements. If the optional parameter order is supplied, and is not zero, then columns are sorted by the elements of a row. Note that a heap sort is used in this routine rather than the system qsort(). Recall that qsort() only works on vectors stored completely in RAM.

Submat() extracts a submatrix from a matrix. The order of the arguments is a little weird, but I wanted to take advantage of default arguments. The bottom right corner is supplied first by the row number, r, and the column number c. The default top left corner is (1,1). You may supply other topmost row in the variable lr, and left most column in the variable lc. Submat() returns a VMatrix reference. The submatrix has top left indices of (1,1).

Vertical and horizontal concatenation is achieved through the functions Cv() and Ch(). Both functions take two matrices as arguments. The matrices must have the same number of columns to be vertically concatenated, and the same number of rows to be horizontally concatenated. Both functions return a VMatrix reference.