How to find common elements in two set and form a trio element set?

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 Comment

What kind of help do you need? What did you try already and which problems occur?

Sign in to comment.

 Accepted Answer

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

2 Comments

but if i want 1 2 to pair with 1 3 only....samewise 1 3 with 1 4 and same will be followed by other bellow pairs, not any other pairs then what shall i do

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 16 Apr 2015

Commented:

on 17 Apr 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!