I want to find duplicate rows in my Nx2 matrix regardless of order. For instance if I were to have a matrix A
[ 1 2;
2 3;
2 1;
3 5;
4 6]
I would like to be told for instance that rows 1 and 3 are the same. Is there a quick and easy way to do this?
Thank you.

 採用された回答

madhan ravi
madhan ravi 2019 年 4 月 8 日

0 投票

Try this:
AA =sort(A,2);
U = unique(AA,'rows');
R = cell(size(AA,1),1);
for k = 1:size(U,1)
R{k}=find(ismember(AA ,U(k,:), 'rows'));
end
Rows=R(cell2mat(cellfun(@(x)numel(x)>=2,R,'un',0)));
celldisp(Rows)

1 件のコメント

Billy Herdman
Billy Herdman 2019 年 4 月 8 日
Works perfectly, thanks!

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

その他の回答 (1 件)

Stephen23
Stephen23 2019 年 4 月 8 日

2 投票

Simpler using hist (or its more recent equivalents):
>> A = [1,2;2,3;2,1;3,5;4,6;5,3] % I added an extra row
A =
1 2
2 3
2 1
3 5
4 6
5 3
>> [U,~,X] = unique(sort(A,2),'rows');
>> [N,E] = hist(X,1:max(X));
>> C = arrayfun(@(x)find(X==x),E(N>1),'uni',0);
>> C{:}
ans =
1
3
ans =
4
6

2 件のコメント

Pedro Guevara
Pedro Guevara 2021 年 1 月 6 日
Hi. I have one. I also need something similar to the author of the Post, however I require that each row be exactly the same (not inverted as requested by the author), that is:
A = [1,2; % This row (1) is exactly the same as (3)
2,3;
1,2; % This row (3) is exactly the same as (1)
3,5; % This row (4) is exactly the same as (6)
4,6;
3,5] % This row (6) is exactly the same as (4)
What would I have to modify in your code to make it do what I need? Thanks.
Stephen23
Stephen23 2021 年 1 月 7 日
"What would I have to modify in your code to make it do what I need?"
Remove the sort function.

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

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by