Intersection of two sets keeping all intersected values

19 visualizzazioni (ultimi 30 giorni)
Say I have two sets a, and b such that:
a=[1 2 3 4 5 6 6 6 7 8 9 10]
b=[1 12 113 1 25 3 6 6 ]
intersect(a,b)= 1, 3, 6
I want to take the intersection of these two sets, and keep all values of intersection (i.e. not just the unique values that the intersection function outputs).
That is, I want:
intersect(a,b)= 1, 3, 6, 6
  1 Commento
Marc Compere
Marc Compere il 27 Mar 2021
this is certainly a valid concern.
there needs to be an option to do this natively. this is not the expected behavior of a set intersection. this is expected behavior of a unique set intersection. unique is ok but there needs to be an option for non-unique similar to 'stable'

Accedi per commentare.

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 15 Lug 2014
a=[1 2 3 4 5 6 6 6 7 8 9 10]
b=[1 12 113 1 25 3 6 6 ]
out=a(ismember(a,b))

Più risposte (1)

Ben11
Ben11 il 15 Lug 2014
Try this:
a(ismember(a,b))
ans =
1 3 6 6 6
6 appears 3 times since it is at 3 locations in a.

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by