2012-08-03 33 views
1

各自的值如何我只是做在MATLAB一个简单的排序。我总是必须使用excel链接导入我的数据,对它进行排序,然后导出回matlab。这很烦人!MATLAB排序一列,并保持在第二列

我有一个矩阵< 10×10>,我想按降序排列,同时保持它的第二列各值的第一列进行排序。 Matlab似乎只是单独对每列进行排序。

Example: 
matrix a 
5 4 
8 9 
0 6 
7 3 

matrix b (output) 
0 6 
5 4 
7 3 
8 9 

回答

8

sortrows答案由@chaohuang可能是你在找什么。但是,它根据所有列进行排序。如果您只想根据第一列进行排序,那么您可以这样做:

% sort only the first column, return indices of the sort 
[~,sorted_inds] = sort(a(:,1)); 

% reorder the rows based on the sorted indices 
b = a(sorted_inds,:); 
+5

这与a = sortrows(a,1)'没有区别...... – 2012-08-03 07:38:45

相关问题