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

조회 수: 12 (최근 30일)
suchismita
suchismita 2015년 4월 16일
댓글: suchismita 2015년 4월 17일
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
Jan
Jan 2015년 4월 16일
What kind of help do you need? What did you try already and which problems occur?

댓글을 달려면 로그인하십시오.

채택된 답변

Thorsten
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');
  댓글 수: 2
suchismita
suchismita 2015년 4월 17일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by