Sort a matrix with another matrix

Posted by Jacob on Stack Overflow See other posts from Stack Overflow or by Jacob
Published on 2010-04-20T22:04:34Z Indexed on 2010/04/20 22:43 UTC
Read the original article Hit count: 281

Filed under:

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)?

E.g.

A = rand(3,4);
[val ind] = sort(A,2);
B = rand(3,4);
%// Reorder the elements of B according the reordering of A

This is the best I've come up with

m = size(A,1);
B = B(bsxfun(@plus,(ind-1)*m,(1:m)'));

Out of curiosity, any alternatives?

© Stack Overflow or respective owner

Related posts about matlab