I've written the following function:
import numpy as np
def _find_nearest(array, value):
"""Find the index in array whose element is nearest to value.
Parameters
----------
array : np.array
The array.
value : number
The value.
Returns
-------
integer
The index in array whose element is nearest to value.
"""
if array.argmax() == array.size - 1 and value > array.max():
return array.size
return (np.abs(array - value)).argmin()
I'd like to vectorize this function, so that I can pass several values at once. That is, I'd like to have value
be an array, and have _find_nearest
return, rather than a single index, the indices for each of the values in the submitted value_array
.
Can anyone see a way to do this?