这是在 Matlab 中删除超集的问题的后续问题。在上一个问题中,我正在删除超集并保留我有工作答案的子集现在我想用子集替换超集而不是删除它们,例如我有一个数据集如下:
a{1} = [5]
a{2} = [4 11 14]
a{3} = [1]
a{4} = [5 16]
a{5} = [5]
a{6} = [11 16]
a{7} = [11]
a{8} = [16]
a{9} = [9 14 17]
a{10} = [14]
[ii, jj] = ndgrid(1:numel(a));
s = cellfun(@(x,y) all(ismember(x,y)), a(ii), a(jj));
% Set diagonal to zero.
s = s - diag(diag(s));
% Indicator matrix for sets that are exactly equal.
same = s & s';
% For equal sets keep only the first occurence.
keep = triu(same) | ~same.*s;
% Delete supersets.
similarity = a(~any(keep,1));
celldisp(similarity)
当我运行上面的代码时,输出如下:
a{1} = [5]
a{2} = [1]
a{3} = [11]
a{4} = [16]
a{5} = [14]
我想做的是用子集替换超集而不是删除它们:
预期输出应如下所示:
a{1} = [5]
a{2} = [11]
a{3} = [1]
a{4} = [5]
a{5} = [5]
a{6} = [11]
a{7} = [11]
a{8} = [16]
a{9} = [14]
a{10} = [14]