Matlab:如何删除具有其他集合作为子集的单元格元素
Matlab: How to remove cell elements which have other sets as subsets
我有一个单元格,其中列出了数组:
C = {[1,2,3,4], [3,4], [2], [4,5,6], [4,5], [7]}
我要输出:
D = {[3,4], [2], [4,5], [7]}
D 中的这些集合是唯一包含 D 中任何其他集合的集合。
类似问题请参考以下。虽然优雅,但我无法修改代码(还)以适应我的特定问题。
如能提供解决方案,我将不胜感激。
谢谢!
您可以简单地通过将每个元素与其下一个元素进行比较并查看是否有任何下一个元素是当前元素的子集来完成此操作,如果是,则删除较大的元素。这是您要查找的简单代码:
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)) = [];
从链接 post 开始,您可以形成矩阵 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]}
我要输出:
D = {[3,4], [2], [4,5], [7]}
D 中的这些集合是唯一包含 D 中任何其他集合的集合。
类似问题请参考以下
如能提供解决方案,我将不胜感激。
谢谢!
您可以简单地通过将每个元素与其下一个元素进行比较并查看是否有任何下一个元素是当前元素的子集来完成此操作,如果是,则删除较大的元素。这是您要查找的简单代码:
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)) = [];
从链接 post 开始,您可以形成矩阵 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)) ;