用另一个矩阵对一个矩阵进行排序 [英] Sort a matrix with another matrix
本文介绍了用另一个矩阵对一个矩阵进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个矩阵A
,并且对该矩阵的行进行了排序.如何在矩阵B
(当然是相同大小)上复制相同的顺序?
Suppose I have a matrix A
and I sort the rows of this matrix. How do I replicate the same ordering on a matrix B
(same size of course)?
例如
A = rand(3,4);
[val ind] = sort(A,2);
B = rand(3,4);
%// Reorder the elements of B according to the reordering of A
这是我想出的最好的
m = size(A,1);
B = B(bsxfun(@plus,(ind-1)*m,(1:m)'));
出于好奇,还有其他选择吗?
Out of curiosity, any alternatives?
更新:乔纳斯的出色解决方案在2008a(XP)上进行了简介:
Update: Jonas' excellent solution profiled on 2008a (XP):
0.048524 1.4632 1.4791 1.195 1.0662 1.108 1.0082 0.96335 0.93155 0.90532 0.88976
n = 2m
0.63202 1.3029 1.1112 1.0501 0.94703 0.92847 0.90411 0.8849 0.8667 0.92098 0.85569
查看全文