How to find unique group of numbers?

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by