在Matlab中删除超集的问题 [英] Issue in deleting supersets in Matlab

查看:163
本文介绍了在Matlab中删除超集的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组由集合组成的数据,我想删除其子集如下的超集:

i've a set of data consisting of sets i want to remove super sets for which subsets are present as follows:

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));
s = triu(s,1); %// count each pair just once, and remove self-pairs
similarity = a(~any(s,1));
celldisp(similarity)

结果如下:

a{1} = [5]
a{2} = [4 11 14]
a{3} = [1]
a{4} = [11 16]
a{5} = [11]
a{6} = [16]
a{7} = [9 14 17]
a{8} = [14]

如输出所示,

仍然有一些超集应删除,即a{2},因为a{5}包含11是它的子集,a{4}应该被删除,因为a{5}包含11a{6}包含16以及a{7}也应删除,因为a{8}包含子集14.

as the output shows there are still supersets that should be removed i.e. a{2} because a{5} contains 11 which is its subset,a{4} should be removed because a{5} contains 11 and a{6} contain 16 as well as a{7} should be deleted too because a{8} contains subset 14.

预期输出为

a{1} = [5] 
a{2} = [1]
a{3} = [11]
a{4} = [16]
a{5} = [14]

谁能帮助您修复此代码,以便我可以获取准确的结果集.谢谢

can anyone help how to fix this code so that i can get accurate set of results. thanks

推荐答案

我认为您需要使用下部三角形而不是上部:

I think you need to use the lower triangular part instead of the upper:

s = tril(s,-1); % instead of s = triu(s,1);

修改

仅当超集始终出现在子集之前时,保留下部三角形部分才起作用.这是一个通用版本,应该总是可以正常工作.

Keeping the lower triangular part only works when the supersets always occur before the subsets. Here is a general version that should always work fine.

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

顺便说一句,运行双循环而不是上面的矩阵运算可能会更容易.

By the way, it might be easier to just run a double loop instead of the above matrix operations.

这篇关于在Matlab中删除超集的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆