intersect function returns only one index why ?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sososasa
le 27 Mar 2014
Réponse apportée : the cyclist
le 27 Mar 2014
Hi,
I have:
A = [1 2 3 4;
1 2 3 4;
5 6 1 2]
B = [1 2]
[~, index] = intersect(A(:,1:2),B(1,:),'rows')
The result I get is:
index = 2
Why is that while I actually have 2 rows in A equal to B
Why I don't get index = 1 and 2
0 commentaires
Réponse acceptée
Walter Roberson
le 27 Mar 2014
C = intersect(A,B) returns the data common to both A and B with no repetitions.
Notice the "no repetitions". The two entries in A become the same after extracting the first two columns, so outputting the entry twice would be a repetition.
If A and B are numeric arrays, logical arrays, character arrays, categorical arrays, or cell arrays of strings, then C = A(ia) and C = B(ib).
Notice there that since C is the same size either way, it follows that ia and ib must be the same length. As only one intersection is being returned for C (because the other is a repetition) it follows that only one index is being returned in ia and ib.
You should be considering ismember()
0 commentaires
Plus de réponses (1)
the cyclist
le 27 Mar 2014
The slightly snarky answer is, "That is not what the intersection function returns."
The slightly nicer answer is that if you read the documentation, you'll see that intersect does not return the indices to all rows that are in the intersection.
To do what you want is easy, though:
C = intersect(A(:,1:2),B(1,:),'rows')
indices = find(ismember(A(:,1:2),C,'rows'))
0 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!