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

13 views (last 30 days)
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...

Accepted Answer

Thorsten
Thorsten on 16 Apr 2015
% 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
suchismita
suchismita on 17 Apr 2015
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

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!