1

I am working on a problem where I quickly need to find the indices of an array (array2) that are closest to the values in another array (array1), with an index for each value of array1. array1 and array2 will never be the same size.

My current function is below:

def findClosestArrayIndices(array1, array2):
    indices = []
    for value in array1:
        indices.append((np.abs(array2 - value)).argmin())
    return np.array(indices)

This works as intended, but is slow because of the for loop going over each value in array1, especially when array1 gets large. Is there a more pythonic/numpy way to rewrite this function to excise the loop and speed up the process?

Thank you

0 Answers0