How to find unique group of numbers?

1 visualización (últimos 30 días)
Leon
Leon el 27 de Ag. de 2020
Comentada: Leon el 27 de Ag. de 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 comentarios
Adam Danz
Adam Danz el 27 de Ag. de 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 el 27 de Ag. de 2020
Works as well. Many thanks, Adam!

Iniciar sesión para comentar.

Respuesta aceptada

Bruno Luong
Bruno Luong el 27 de Ag. de 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{:}

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by