I need to find the index of the first element in an array, that is close to a float within a given tolerance.
I can do this with a for block:
import numpy as np
# Define array of floats
a = np.random.uniform(0., 10., 10.)
# Float to search, and tolerance.
val, tol = 6.87, 0.1
for i, _ in enumerate(a):
if np.isclose(_, val, tol):
print('Index is: {}'.format(i))
break
but I was wondering if there might be a one-liner solution using some numpy function.
Notice that I'm interested in the index of the first element that is close to val, no matter that there might be closer elements further down the a array. The solutions I've found are interested in the index of the nearest value, no matter where it is located within the array.
Here's a one-liner:
Index = next(i for i, _ in enumerate(a) if np.isclose(_, val, tol))
The code in parentheses is a generator expression and next returns (you guessed it!) the next (in this case, the first) value the generator will produce. If there'll be no next value, a StopIteration exception will be raised.
It could be easily turned into a one-liner function:
FirstIndex = lambda a, val, tol: next(i for i, _ in enumerate(a) if np.isclose(_, val, tol))
i = FirstIndex(a, val, tol) # call it
Here's a vectorized one-liner -
(np.abs(a - val) <= tol).argmax()
Sample step-by-step run -
In [57]: a
Out[57]: array([5, 3, 9, 6, 8, 3, 5, 1])
In [58]: val = 2
In [59]: tol = 2
In [60]: (np.abs(a - val) < tol) # Many might satisfy
Out[60]: array([False,True,False,False,False,True,False,True], dtype=bool)
In [61]: (np.abs(a - val) <= tol).argmax() # Choose the first one with argmax
Out[61]: 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With