Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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?

Risposte (1)

KSSV
KSSV il 8 Set 2016
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.

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by