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

5 ビュー (過去 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 日
thank u so much....
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 Center および File ExchangeElementary Math についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by