我有一个单元格,其中列出了数组:
C = {[1,2,3,4], [3,4], [2], [4,5,6], [4,5], [7]}
我要输出:
D = {[3,4], [2], [4,5], [7]}
D中的那些集合是唯一包含D中其他任何集合的集合。
关于类似问题,请参考以下link。尽管很优雅,但我仍无法修改代码以适应我的特定问题。
我非常感谢您提供解决方案的帮助。
谢谢!
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)) = [];