drsealks
drsealks

Reputation: 2354

Reverse sort of matrix columns

Say, if I do this for some matrix A:

[sorted,inds] = sort(A,1,'descend')

How do I do a reverse sort of this matrix?

I need something like this: http://blogs.mathworks.com/loren/2007/08/21/reversal-of-a-sort/#7

Any ideas?

Thank you

Upvotes: 1

Views: 51

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

A = [8 4 6 8;3 2 5 6;9 3 4 5];
[sorted,inds] = sort(A,1,'descend')

B = NaN(size(A));
B(bsxfun(@plus, inds, 0:size(A,1):numel(A)-1)) = sorted;

gives B equal to A.

The trick is that inds should be interpreted as column indices. You need to convert to linear indices, which is easily done with bsxfun.

Upvotes: 2

Related Questions