intersect function returns only one index why ?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
the cyclist
el 27 de Mzo. de 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'))
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!