How to reorder the rows by sorting the elements of column in ascending order?

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.

 채택된 답변

dpb
dpb 2014년 5월 13일
편집: dpb 2014년 5월 13일
for c=1:3
is(1)={sortrows(id,i)};
end
using a cell array to hold results; use a dynamic named structure field if desired
Alternatively, arrayfun is your friend for such things...
idsrted=arrayfun(@(c) sortrows(id,c),[1:3],'uniform',0);

댓글 수: 4

Hi dpb, I preferred the first method that suggested by you :)As follows:
id=[ 1 3 2; 2 6 4; 3 2 6; 4 5 1; 5 1 3; 6 4 5; 7 7 7];
for c=1:3
id=sortrows(id,c);
end
There result will give me 3 sets of numbers. If I want to call for the first set after that, what should I add?
Thanks.
Your loop above only gives one value and at the end of the loop you've only got the result as if you had simply done
id=sortrows(id,3);
Note, in my loop solution, as in the arrayfun one, there is a cell array, each containing the array in its sorted condition. This has the advantage of having them all available, but is duplicating the memory two extra times. For such small (I presume sample) arrays that's no biggie; in your real problem that might become an issue.
Why don't you do what you need with the array sorted by the selected column after having done so, then go on to the next?
But, to answer (finally :) ) the question, you address the cell array using the {} ("curlies") to retrieve the content of the cell.
hi dpb, it is because later i will use the same code to do a bigger array, so I'm now trying use the small array first see whether it works or not. :)
But then,how to use the curlies to retrieve the content of the cell?
id=[1 3 2;2 6 4;3 2 6; 4 5 1; 5 1 3; 6 4 5; 7 7 7];
for c=1:3
new_id=sortrows(id,c);
end
if use this as an example, the output will give me three different new_id, and I want to retrieve first set of new_id, where i need to put as curlies? Is this what u meant?
for c=1:3
new_id=sortrows(id,c);
...if use this as an example, the output will give me three different new_id...
NO! It'll give you only one new_id; the last one. Each subsequent pass thru the loop overwrites the previous as you didn't increment a cell array or concatenate the results to the existing in a single larger array.
Again, it's not clear just what you're after--if doesn't matter how large the array is as to the logic; the question remains on whether you must have the results all at once or can do the process on each subset at a time. We can't answer that; that depends on the results required. The thing I wonder about is why do sortrows individually if the latter; why not concatenate the full dataset then sort only once at the end? Bound to be more efficient.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

[~,ii] = sort(id);
id_out = bsxfun(@(a,b)a(b,:),id,permute(ii,[1 3 2]));

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2014년 5월 13일

댓글:

dpb
2014년 6월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by