我有一个单元格,里面列出了数组:
C = {[1,2,3,4], [3,4], [2], [4,5,6], [4,5], [7]}
我想输出:
D = {[3,4], [2], [4,5], [7]}
D中的那些集合是唯一本身包含D中任何其他集合的集合。
类似问题请参考以下链接。虽然很优雅,但我(还(无法修改代码以适应我的特定问题。
如果能为我提供解决方案,我将不胜感激。
谢谢!
作为链接帖子,您可以形成矩阵s
,该矩阵表示所有集合对之间相似元素的数量。结果是:
C = {[1,2,3,4], [3,4], [2], [4,5,6], [4,5], [7]};
n = cellfun(@numel,C); % find length of each element.
v = repelem(1:numel(C),n); % generate indices for rows of the binary matrix
[~,~,u] = unique([C{:}]); % generate indices for rows of the binary matrix
b = accumarray([v(:),u(:)],ones(size(v)),[],@max,[],true); % generate the binary matrix
s = b * b.'; % multiply by its transpose
s(1:size(s,1)+1:end) = 0; % set diagonal elements to 0(we do not need self similarity)
result=C(~any(n(:) == s)) ;
但是矩阵可能很大,所以最好使用循环来避免内存问题:
idx=false(1,numel(C));
for k =1:numel(C)
idx(k) = ~any(n == full(s(k, :))) ;
end
result=C(idx) ;
或者采用矢量化方法:
[r, c, v] = find(s) ;
idx = sub2ind(size(s), r, c) ;
s(idx) = v.' == n(r) ;
result = C(~any(s)) ;
您可以简单地通过将每个元素与其下一个元素进行比较来实现这一点,并查看下一个元件中是否有任何一个是当前元件的子集,如果是,则删除较大的元件。这里有一个简单的代码,可以做你正在寻找的事情:
C = {[1,2,3,4], [3,4], [2], [4,5,6], [4,5], [7]};
% Initialize D with a copy of C
D = C;
% Compare each element i with other elements j = i+1, i+2, ....
for i = 1:numel(C)-1
for j = i+1:numel(C)
% Check to see if C{j} exists in C{i}
if contains(num2str(C{i}),num2str(C{j}))
% Make unwanted elements empty
D{i} = [];
end
end
end
% Remove empty elements
D(cellfun(@isempty,D)) = [];