What may be the best way to order columns of a matrix?

I'd like to order each column for a matrix so that the smallest number is replaced with 1, the second smallest number is replaced with 2 and so on. Say, convert mat to mat2. Any way to do this fast?
The code for before-converted matrix: mat = [20 1 4;5 3 1;-3 1 9;9 1 1];

1 Comment

PLEASE DO NOT PASTE IN AN IMAGE. That makes it impossible for us to use your array without typing it in ourselves.

Sign in to comment.

Answers (1)

The second return value of sort tells you which row should receive 1,2,3,4... You can use sub2ind, bsxfun or a loop to distribute the values:
mat = [20 1 4;5 3 1;-3 1 9;9 1 1];
[~, order] = sort(mat);
mat2(sub2ind(size(mat), order, repmat(1:size(mat, 2), size(mat, 1), 1))) = repmat((1:size(mat, 1))', 1, size(mat, 2));
mat2 = reshape(mat2, size(mat))

5 Comments

Could you explain your code a bit? Also, do you have other methods that use less functions? This is fancy but too much techniques
The code is explained a bit. The first line gets the second return value of sort. The second line just distribute 1, 2, ..., nrows into the rows indicated by that 2nd returned value.
There's hardly any technique there, and if you want to do efficient data manipulation you'd better learn them.
There are plenty of other ways to achieve what you want. You could also loop over the columns and do:
[~, order] = sort(column);
newcolumn(order) = 1:numel(column);
My code simply does this all at once for all columns.
For your convenience, I added the for loop around Guillaume's second suggestion:
for i = 1:size(mat, 2),
[~, ind] = sort(mat(:,i));
mat2(ind,i) = 1:size(mat,1);
end
@Guillaume, the way Matlab uses matrix as parameter is interesting. Inspired from your code, mat2([2 4; 1 3])=[1 -1;20 2], for instance, I guess is assigning value from right side to the left side based on corresponding index and the result of mat2 is then a 1*4 vector [20 1 2 -1]. But what if I write mat2([2 4 1 3])=[1 -1;20 2]? What will I get?
There is/(was?) a page in the documentation that describes the exact rules for indexing. I can't find it at the moment.
The rule is rather ill-designed in my opinion as it has exceptions, but in general:
A(I)
has the same shape as I and the shape of A is lost. Exceptions come when both are vector, then it's the shape of A.
If A does not exist prior to assignment, then it would appear that A is always a row vector.
The shape of the assigned is always lost, and values are assigned in linear order, that is by column. So in your example, [1 -1; 20 2], the values are flattened in the order [1 20 -1 2].
Similarly in mat2([2 4; 1 3]), the indices are flattened into [2 1 4 3]. In the other case, the indices are already a vector.

Sign in to comment.

Categories

Asked:

on 3 Dec 2015

Commented:

on 3 Dec 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!