【问题标题】:How to replace supersets with there subsets in matlab?如何用matlab中的子集替换超集?
【发布时间】:2015-04-17 10:07:43
【问题描述】:

这是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]

【问题讨论】:

  • 请解释预期的输出。为什么a{4} 5 而不是 16?这很重要还是应该选择任何现有的子集?
  • 因为在循环中单个值 5 在我们从上到下时首先出现,我们从对开始,如果在 a{4} 中有 16 比它下降到单个 selements将是 16 在位置 a{4}
  • 不清楚你在问什么。获得该输出的确切规则是什么?
  • @LuisMendo 规则很简单,if there is a super set and its subset is present replace superset with its sub set 例如a{1}=[1 2 3]a{2}= [2 3] 所以将a{1} 替换为a{2}。这应该为每个a{n}

标签: matlab subset superset


【解决方案1】:

试试这个:

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);

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2015-07-09
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 2015-07-11
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多