MATLAB技巧——sort和sortrows函数

1、sort函数

sort函数用于对数据进行排序，通过help sort命令，可以查找到sort函数的具体用法：

Y = SORT(X,DIM,MODE)
has two optional parameters.
DIM selects a dimension along which to sort.
MODE selects the direction of the sort
'ascend' results in ascending order
'descend' results in descending order
The result is in Y which has the same shape and type as X.

A=\begin{pmatrix} 5 & 7 & 8 \\ 4 & 6 & 1 \\ 8 & 0 & 7 \end{pmatrix}

2、sortrows函数

SORTROWS(X,COL) sorts the matrix based on the columns specified in the
vector COL.  If an element of COL is positive, the corresponding column
in X will be sorted in ascending order; if an element of COL is negative,
the corresponding column in X will be sorted in descending order. For
example, SORTROWS(X,[2 -3]) sorts the rows of X first in ascending order
for the second column, and then by descending order for the third
column.

sortrows函数根据列col升序排序：

297 篇文章60 人订阅

0 条评论