フィルターのクリア

How I manually write a function for computing set intersection? (partial code)

2 ビュー (過去 30 日間)
Tyler Johnson
Tyler Johnson 2016 年 9 月 7 日
回答済み: KSSV 2016 年 9 月 8 日
I am trying to manually write the intersect function that takes two sets and produces the values that are the same within both. for example: {a, b, c} and {a, b, g} should produce a, b. However when I run my code I am just getting a set full of commas as output like this: { , , , , , , , ,}. My code is as follows:
function result = TylerJohnsonIntersect(set1, set2)
uset1 = unique(set1);
uset2 = unique(set2);
for i = 1:length(uset1)
for j = 1:length(uset2)
if uset1{i} == uset2{j}
set3{i} = i;
end
end
end
result = set3;
end
Can anyone point me in the right direction?

回答 (1 件)

KSSV
KSSV 2016 年 9 月 8 日
How about this?
function result = TylerJohnsonIntersect(set1, set2)
uset1 = unique(set1);
uset2 = unique(set2);
for i = 1:length(uset1)
for j = 1:length(uset2)
if uset1{i} == uset2{j}
set3(i) = i;
end
end
end
result = set1(set3);
There is command intersect(A,B) in MATLAB. Have a look on that.

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by