MATLAB: Sorting a Matrix using Indices from another matrix !

columnwise sortingmatrix sortingsort matrix using matrix

Hello
I have a pretty simple question !
I have a matrix A (lets assume has a size 5 x 5) and I have another matrix of same size (5 x 5) called IDX that contains the values for sorting A in a colum-wise manner.
So how can I easily sort each column of A using matrix IDX without using a for loop or extracting each column of IDX as an individual vector ?
Thank you in advance !

Best Answer

[m,n]=size(A);
A=A(sub2ind([m n],idx,repmat(1:n,m,1)))