matlab的sort函数求问

我的问题是能不能像C++中的sort函数那样,把每一行当做一个整体来排序
比如说,我有A =

[2 8;5 7;1 6]
现在我想仅仅按照第一列来排序,但是同时把第二列当做一个整体改变,
比如按照第一列升序排列后应该变为
A= [1 6; 2 8; 5 7]
但是sort函数现有的用法好像不能实现这个功能。

不太想用类或者结构体,就一个简单的程序居然还得写类

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(A, 1)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-27
先用for循环把矩阵的每一行提取出来成为一个单独的行向量,对提取出来的行向量进行求和,比较和值大小,然后根据和的大小从上到下依次将单独的行向量排列下去(还得用个for语句),构成一个新的矩阵,这个矩阵就是你所要求的了
相似回答