How to find common elements in two set and form a trio element set?
15 次查看(过去 30 天)
显示 更早的评论
I have 8 pair of sets, for example,
{1,2},{1,3},{1,4},{2,4},{2,5},{3,4},{3,5},{4,5}
this two element of two set will be compared and they will form a trio element set under three conditions:
1)first element or second element must be common between two sets. For example, in between {1,2} and {1,3} 1 is common so they will form {1,2,3}.
2)if there are no common element between two sets then they would not form any trio set.
3)if the new formed trio set is found earlier then also it won't be counted.
the pattern of check common element will be serial wise.
for example:
{1,2} and {1,3} = {1,2,3};
{1,3} and {1,4} = {1,3,4};
{1,4} and {2,4} = {1,2,4};
{2,4} and {2,5} = {2,4,5};
{2,5} and {3,4} = as no common element so dismiss
{3,4} and {3,5} = {3,4,5};
{3,5} and {4,5} = as 5 is common and {3,4,5} is found above so dismiss
please help me with this coding.... plz plz...
1 个评论
采纳的回答
Thorsten
2015-4-16
% define the pairs
X = [1,2;1,3;1,4;2,4;2,5;3,4;3,5;4,5];
% compute indices of all possible combination of the pairs
allpairs = nchoosek(1:size(X, 1), 2);
% X3 stores the triplets
X3 = [];
for i=1:size(allpairs, 1)
uX = union(X(allpairs(i,1),:), X(allpairs(i,2), :));
if numel(uX) == 3
X3(end+1,:) = uX;
end
end
% remove doublicates
X3 = unique(X3, 'rows');
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!