How can I find the indices of an array with float values in python?
Rob Gaddi
rgaddi at highlandtechnology.invalid
Thu Jan 10 13:09:53 EST 2019
On 1/10/19 9:25 AM, Peter Otten wrote:
> Madhavan Bomidi wrote:
>
>> I have an array (numpy.ndarray) with shape (1500L,) as below:
>>
>> x = array([ 3.00000000e+01, 6.00000000e+01, 9.00000000e+01, ...,
>> 4.49400000e+04, 4.49700000e+04, 4.50000000e+04])
>>
>> Now, I wanted to determine the indices of the x values between 0.0 and
>> 15.0. While this is simple in MATLAB or IDL by using find or where
>> functions, I was unable to find a best way to find the indices of all
>> elements between 0.0 and 15.0 in the x array. Can you please suggest me a
>> solution for the same?
>
> Like this?
>
>>>> a = numpy.array([-1, 10, 100, 5, 1000.])
>>>> numpy.where((a < 15) & (a > 0))[0]
> array([1, 3])
>
I was going to suggest numpy.nonzero, which gives the same result as the
simple case of numpy.where as above. In either case, the result is a
tuple of indices per axis.
>>> a = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
>>> a = np.array([[2, 7, 6], [9, 5, 1], [4, 3, 8]])
>>> b = np.array([1, 5, 2, 6, 3, 7, 4, 8, 5, 9])
>>> (a > 5).nonzero()
(array([0, 0, 1, 2]), array([1, 2, 0, 2]))
>>> np.nonzero(b % 2)
(array([0, 1, 4, 5, 8, 9]),)
--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order. See above to fix.
More information about the Python-list
mailing list