如何用matlab中的子集替换超集?

时间:2015-04-17 10:07:44

标签: matlab subset superset

这是Issue in deleting supersets in 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]

1 个答案:

答案 0 :(得分:0)

试试这个:

m = arrayfun(@(i,j) all(ismember(a{i}, a{j})), ii, jj); %// subset relationship 
[valid, ind] = max(tril(m, -1)); %// consider only other sets with higher index
ind1 = find(valid); %// these will be replaced...
ind2 = ind(valid); %// ...by these ones
a(ind1) = a(ind2);