假设我有一个矩阵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
Run Code Online (Sandbox Code Playgroud)
这是我提出的最好的
m = size(A,1);
B = B(bsxfun(@plus,(ind-1)*m,(1:m)'));
Run Code Online (Sandbox Code Playgroud)
出于好奇,还有其他选择吗?
0.048524 1.4632 1.4791 1.195 1.0662 1.108 1.0082 0.96335 0.93155 0.90532 0.88976
Run Code Online (Sandbox Code Playgroud)
0.63202 1.3029 1.1112 1.0501 0.94703 0.92847 0.90411 0.8849 0.8667 0.92098 0.85569
Run Code Online (Sandbox Code Playgroud)
它只是表明,由于JITA(或许),循环不再是MATLAB程序员的诅咒.