views:

36

answers:

0

Possible Duplicate:
Sort a matrix with another matrix

Given two matrices A and B of the same size, I want to sort A across the second dimension (rows) and apply the same ordering to the matrix B. Is it possible to vectorize this current code?

r = 10; c = 4;
A = rand(r,c);
B = reshape(1:r*c,c,r)';  % can be any random matrix'

[A,order] = sort(A,2);
for i=1:r
    B(i,:) = B(i,order(i,:));
end