Compare two arrays of different length

I have two arrays. Lets say A = [1 5 7 10 14 17 19 22 25 26 29], B= [2 7 12 18 23 ] I want to find out the values / positions in A which are nearest to every value of B.
P.S : The resulting array should have the same length as B. Any suggestions are welcome.
Thanks

 Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Dic. de 2012
[junk, Aidx] = min(abs(bsxfun(@minus,A.',B)));

3 comentarios

Joe Abraham
Joe Abraham el 18 de Dic. de 2012
awesome. can u please tell me what this code does?
Jan
Jan el 18 de Dic. de 2012
@Joe: Simply try it by splitting into parts:
A
C1 = A.'
C2 = bsxfun(@mtimes, C1, B)
C3 = abs(C2)
[C4, C5] = min(C3)
Look in the documentation for each command:
help transpose
help bsxfun
help mtimes
help abs
help min
Or read the more exhaustive documentation by replacing "help" with "doc".
Walter Roberson
Walter Roberson el 18 de Dic. de 2012
@minus rather than @mtimes, but yes.

Iniciar sesión para comentar.

Más respuestas (1)

kate hughes
kate hughes el 21 de Oct. de 2014

0 votos

Just to be clear this doesn't work on matrices or arrays of different size, does anyone know what does?

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 18 de Dic. de 2012

Respondida:

el 21 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by