Monday, 9 September 2013

Find float in ndarray

Find float in ndarray

I tried to find a float number in ndarray. Due to the software package I
am using (Abaqus), the precision it outputs is a little bit low. For
example, 1.6 is something like 10.00003. Therefore, I was wondering
whether there is a "correct" way to do it, that is neater than my code.
Example code:
import numpy as np
array = np.arange(10)
number = 5.00001
if I do thisF
idx = np.where(number==array)[0][0]
Then the result is empty because 5.00001 does not equal to 5.
Now I am doing:
atol = 1e-3 # Absolute tolerance
idx = np.where(abs(number-array) < atol)[0][0]
which works, and is not too messy... Yet I was wondering there would be a
neater way to do it. Thanks!
PS: numpy.allclose() is another way to do it, but I need to use number *
np.ones([array.shape[0], array.shape[1]]) and it still seems verbose to
me...

No comments:

Post a Comment