How to find unique group of numbers?

3 views (last 30 days)
Leon
Leon on 27 Aug 2020
Commented: Leon on 27 Aug 2020
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 Comments
Adam Danz
Adam Danz on 27 Aug 2020
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 on 27 Aug 2020
Works as well. Many thanks, Adam!

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 27 Aug 2020
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{:}

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!