'check if number is between row of numpy array

Want to check if value is between the row of array. here the value 347 is in between the 1st row of aa but not second , so how to check for it ?

aa= np.array([[348 ,345],
              [460 , 459 ]])

value = 347

print(value> aa[:,0]) ==  (value < aa[:,1]) ## help here



answer must be

[True False]


Solution 1:[1]

You need to use != or ^ to get [True False].

But your question is asking for something completely different from the answer you are trying to get.

First, note that aa[:,0] is [348, 460] and a[:,1] is [345, 459]. So to have anything True, I think you to use a[0,:] and a[1,:] (note transposition of : with 0 or 1 -- order matters).

Now with this transposition we have that value > aa[0,:] is [False, True] and not [True, False] and value < aa[:,1] is [True True]. Your first answer should be False since 347 is not between 348 and 460 and your second should be True.

So to put it all together:

((value > aa[0,:])) == (value < aa[1,:])
#array([False,  True])

UPDATE based on your comment to your question above

I just saw your comment and I think I misunderstood what you are trying to do. Sorry I had misunderstood, but it speaks also to the importance of clearly explaining what you are trying to do in your quesiton.

If I now understand correctly, the problem is that the numbers in your row are not necessarily in the right order, so you need to check for min and max values. You can use the axis keyword in np.aminand np.amax, which you can call as np.amin(aa, axis = 1) or aa.min(axis = 1) (same for max). So your answer could look something like:

(value > aa.min(axis = 1)) == (value < aa.max(axis = 1))
#array([ True, False])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1