The Python Oracle

select closest values from two different arrays

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping

--

Chapters
00:00 Question
00:49 Accepted answer (Score 7)
01:02 Thank you

--

Full question
https://stackoverflow.com/questions/1871...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #numpy

#avk47



ACCEPTED ANSWER

Score 7


>>> K = abs(A - C) < abs(B - C)  # create array of bool
[[True, False, False],
 [True,  True, False],
 [False, False, False]]
>>> D = where(K, A, B)     # get elements of A and B respectively