필터 지우기
필터 지우기

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 CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by