Find first common element in 2 vectors
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ryan
el 4 de Abr. de 2022
Comentada: ryan
el 4 de Abr. de 2022
I have 2 vectors, A and B, with identical values but in different orders. How do I find the first common element between them? For example, I have 2 vectors as shown below. How would I return the value "25"?
A = [10 25 36 41 53]
B = [53 41 25 10 36]
Thanks in advance for any help!
4 comentarios
Matt J
el 4 de Abr. de 2022
I want index 25 because it is the first common index that is smallest in the two vectors.
No, 10 is smaller than 25.
Respuesta aceptada
David Hill
el 4 de Abr. de 2022
for k=1:length(A)
c=intersect(A(1:k),B(1:k));
if ~isempty(c)
break;
end
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Descriptive Statistics en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!