Arrange a matrix with repeated rows

Hi,experts
I have a raw matrix as:
a = [125 97
126 104
97 125
104 126];
I'm looking for an algorithm to manipulate this matrix with an output as:
a = [125 97
97 125
126 104
104 126];
just I want to put repeated row together. This is a small piece of a large matrix, but the structure is the same.
Thank you Kyle

 採用された回答

José-Luis
José-Luis 2012 年 12 月 2 日

0 投票

[idx idx] = sortrows(sort(a,2));
a = a(idx,:);

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 2 日
編集済み: Azzi Abdelmalek 2012 年 12 月 2 日

0 投票

a = [125 97;126 104;97 125;5 4;104 126;97 125;4 5];
b=cell2mat(arrayfun(@(x) sort(a(x,:)),1:size(a,1),'un',0)')
[~,idx]=sortrows(b)
out=a(idx,:)
%If you don't need it to be sorted
b=cell2mat(arrayfun(@(x) sort(a(x,:)),1:size(a,1),'un',0)')
[c,d,e]=unique(b,'rows','stable')
[~,idx]=sort(e);
out=a(idx,:)

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2012 年 12 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by