How to find unique group of numbers?

3 ビュー (過去 30 日間)
Leon
Leon 2020 年 8 月 27 日
コメント済み: Leon 2020 年 8 月 27 日
I have a cell array like the below, storing some groups of numbers:
C{1} = [1; 3; 4];
C{2} = [3; 9; 135; 278];
C{3} = [6; 89; 256; 803; 6987; 2356];
C{4} = [356; 799; 2567; 5680];
C{5} = [4; 1; 3];
...
There are some duplicates. For example, C{1} and C{5} are duplicates. How do I remove the duplicates from this array?
Many thanks!
  3 件のコメント
Adam Danz
Adam Danz 2020 年 8 月 27 日
There are lots of ways to do this.
C{1} = [1; 3; 4];
C{2} = [3; 9; 135; 278];
C{3} = [6; 89; 256; 803; 6987; 2356];
C{4} = [356; 799; 2567; 5680];
C{5} = [4; 1; 3];
C{6} = [356; 799; 2567; 5680];
Cs = cellfun(@(v){sort(v)}, C);
[a,b] = meshgrid(1:numel(C),1:numel(C));
pairs = unique(sort([a(:),b(:)],2), 'rows');
pairs(pairs(:,1)==pairs(:,2),:) = [];
isDuplicate = arrayfun(@(i,j)isequal(Cs{i},Cs{j}),pairs(:,1),pairs(:,2));
C(pairs(isDuplicate,2)) = [];
Leon
Leon 2020 年 8 月 27 日
Works as well. Many thanks, Adam!

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

採用された回答

Bruno Luong
Bruno Luong 2020 年 8 月 27 日
I propose do it this way:
% Test example
C{1} = [1; 3; 4];
C{2} = [3; 9; 135; 278];
C{3} = [6; 89; 256; 803; 6987; 2356];
C{4} = [356; 799; 2567; 5680];
C{5} = [4; 1; 3];
s1 = cellfun('size',C,1);
m = max(s1);
d = cellfun(@(a) [length(a), sort(a).', inf(1,m-length(a))], C, 'unif', 0); % First row is the length, Pad the tail with inf
[~,i] = unique(cell2mat(d(:)),'stable','rows');
C = C(i); % Here is the final cells to be kept
% Display
C{:}

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by