How to reorder the rows by sorting the elements of column in ascending order?
Show older comments
Hi everyone,
I have id=[ 1 3 2; 2 6 4; 3 2 6; 4 5 1; 5 1 3; 6 4 5; 7 7 7]
j=1:3
I want to reorder the rows of id by sorting the jth column of id such that the elements in this column are sorted from small to large.
Where my result will get j set of number.
id1=[ 1 3 2; 2 6 4; 3 2 6; 4 5 1; 5 1 3; 6 4 5; 7 7 7]
id2=[5 1 3; 3 2 6; 1 3 2; 6 4 5; 4 5 1; 2 6 4; 7 7 7]
id3=[4 5 1; 1 3 2; 5 1 3; 2 6 4; 6 4 5; 3 2 6; 7 7 7 ]
How can i do this?
Thanks.
Accepted Answer
More Answers (1)
Andrei Bobrov
on 13 May 2014
[~,ii] = sort(id);
id_out = bsxfun(@(a,b)a(b,:),id,permute(ii,[1 3 2]));
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!