Sort rows in ascending order
Syntax
B = sortrows(A)
B = sortrows(A,column)
[B,index] = sortrows(A)
Description
B = sortrows(A)
sorts the rows of A as a group in ascending order. Argument A must be either a matrix or a column vector.
For strings, this is the familiar dictionary sort. When A is complex, the elements are sorted by magnitude, and, where magnitudes are equal, further sorted by phase angle on the interval
.
B = sortrows(A,column)
sorts the matrix based on the columns specified in the vector column. For example, sortrows(A,[2 3]) sorts the rows of A by the second column, and where these are equal, further sorts by the third column.
[B,index] = sortrows(A)
also returns an index vector index.
If A is a column vector, then B = A(index).
If A is an m-by-n matrix, then B = A(index,:).
Examples
Given the 5-by-5 string matrix,
A = ['one ';'two ';'three';'four ';'five '];
The commands B = sortrows(A) and C = sortrows(A,1) yield
B = C =
five four
four five
one one
three two
two three
See Also
sort Sort elements in ascending order
[ Previous | Help Desk | Next ]