假设我有一个矩阵A
,我对该矩阵的行进行排序。如何在矩阵上复制相同的顺序B
(当然大小相同)?
例如
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)'));
出于好奇,还有其他选择吗?
更新: Jonas在 2008a (XP) 上的出色解决方案:
n = n
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
它只是表明循环不再是 MATLAB 程序员的诅咒,这要归功于JITA(也许)。