I have two arrays A and B. Let them both be one-dimensional for now.
For each element in A I need the index of the element in B that best matches the element in A.
I can solve this using a list expression
import numpy as np
A = np.array([ 1, 3, 1, 5 ])
B = np.array([ 1.1, 2.1, 3.1, 4.1, 5.1, 6.1 ])
indices = np.array([ np.argmin(np.abs(B-a)) for a in A ])
print(indices) # prints [0 2 0 4]
print(B[indices]) # prints [1.1 3.1 1.1 5.1]
but this method is really slow for huge arrays.
I am wondering if there is a faster way utilizing optimized numpy functions.