How to find unique group of numbers?

1 visualizzazione (ultimi 30 giorni)
Leon
Leon il 27 Ago 2020
Commentato: Leon il 27 Ago 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 Commenti
Adam Danz
Adam Danz il 27 Ago 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 il 27 Ago 2020
Works as well. Many thanks, Adam!

Accedi per commentare.

Risposta accettata

Bruno Luong
Bruno Luong il 27 Ago 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{:}

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by