这是我想要的一个小例子.给出以下数组:
1 1 2
2 2 1
1 1 1
1 1 6
Run Code Online (Sandbox Code Playgroud)
排序(行总和显示在括号中):
1 1 6 (8)
2 2 1 (5)
1 1 2 (4)
1 1 1 (3)
Run Code Online (Sandbox Code Playgroud)
有没有快速的方法来实现这一点在Matlab?
由于sort按顺序返回索引以及排序矩阵,您可以使用这些索引来访问原始数据 - 试试这个:
% some data
A = [
1 1 2;
2 2 1;
1 1 1;
1 1 6;
];
% compute the row totals
row_totals = sum(A,2);
% sort the row totals (descending order)
[sorted, row_ids] = sort(row_totals, 'descend');
% and display the original data in that order (concatenated with the sums)
disp([A(row_ids,:), row_totals(row_ids)])
>>>
1 1 6 8
2 2 1 5
1 1 2 4
1 1 1 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |