python - Find if a sorted array of floats contains numbers in a certain range efficiently -


i have sorted numpy array of floats, , want know whether array contains number in given range. note i'm not interested in positions of number in array. want know if there @ least 1 number falls in range.

since have same operation on large numbers of intervals (the array remains constant during entire operation), i'm concerned efficiency.

can me?

as talked in comments, np.searchsorted useful , indeed is. stated in question, array stays same, while have different ranges across iterations. so, let's ranges stored in (n,2) shaped array, such first column represents start, while second column stop values of ranges.

we have solution np.searchsorted, -

np.searchsorted(a,ranges[:,0]) != np.searchsorted(a,ranges[:,1]) 

the idea if there's number within range, sorted indices found using first column (start values) lesser , not equal indices second column (stop values).

sample run -

in [75]:   # input data array out[75]:  array([ 13.,  20.,  22.,  24.,  36.,  50.,  52.,  60.,  64.,  65.,  65.,         66.,  72.,  76.,  81.,  84.,  88.,  88.,  90.,  97.])  in [76]: ranges # array of ranges out[76]:  array([[ 19.,  26.],        [ 22.,  33.],        [ 25.,  35.],        [ 38.,  62.]])  in [77]: np.searchsorted(a,ranges[:,0]) != np.searchsorted(a,ranges[:,1]) out[77]: array([ true,  true, false,  true], dtype=bool) 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -