Help: sort a m by n matrix

2 ビュー (過去 30 日間)
Khanh
Khanh 2014 年 9 月 17 日
コメント済み: Khanh 2014 年 9 月 17 日
Hi,
I have a 4 by 2 matrix. c=[2205 300; 7281 300; 3535 600; 5944 600] and want to sort the 1st colum and the 2nd colum by the 1st one. The result I want to obtain is a 4 by 2 matrix like this c'= [2205 300; 3535 600; 5944 600; 7281 300].
I also wrote a script to do it. For convenience, is there a buit-in function MATLAB to do it?
My script:
for i=1:size(c,1)
if i<size(c,1)
if c(i,1)>c(i+1,1)
xiold=c(i,1)
xi1old=c(i+1,1)
yiold=c(i,2)
yi1old=c(i+1,2)
c(i,1)=xi1old
c(i+1,1)=xiold
c(i,2)=yi1old
c(i+1,2)=yiold
end
end
end
Khanh

採用された回答

Hikaru
Hikaru 2014 年 9 月 17 日
Yes, there is a built-in function.
c1 = sortrows(c,1)
  2 件のコメント
Roger Stafford
Roger Stafford 2014 年 9 月 17 日
That would leave the second column unsorted. It should either be
c1 = sortrows(c);
or
c1 = sortrows(c,[1,2]);
Khanh
Khanh 2014 年 9 月 17 日
Thanks all.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by