按降序排序MATLAB数组

Osc*_*ruz 4 indexing matlab matrix

我正在使用MATLAB.我有一个问题,关于如何按矩阵的一列按降序排序矩阵,并使其余的行值遵循.

例如,

A = [1 30; 2 40; 3 10; 4 50; 5 20]
Run Code Online (Sandbox Code Playgroud)

B = [4 50; 2 40; 1 30; 5 20; 3 10]
Run Code Online (Sandbox Code Playgroud)

谢谢

Bob*_*ore 14

使用SORTROWS函数,指定要按降序使用第二列进行排序...

B = sortrows(A, -2);
Run Code Online (Sandbox Code Playgroud)

在此,2分表示"排序在第2栏,",并在2个装置的前面的负号"在 -scending顺序".有关更多信息,请参见MATLAB文档.