I have a sorted numpy array X and also two constants k and delta that are not in X. I would like to find the index corresponding to, the largest value in X less than or equal to k and the value must be within delta of k i.e. I want
max {i | k - delta <= X[i] <= k } (1)
Note this set may be empty in which case I will return None. The way I'm doing it I currently feel is unoptimal as it doesn't take advantage of the fact X is ordered at the first step
# Get the max from the set of indices in X satisfying (1)
idx = np.where((k-delta <= X) * (X <= k))[0].max()
I'm not sure how clever Numpy can be when doing this as it doesn't already know X is sorted hence the (k-delta <= X) * (X <= k)) will I assume take longer than necessary. Note we can use the .max() as we know ourselves the array is sorted.
What would be a more optimal way of doing this?
k? That fact greatly simplifies the solution.kis not inXso I must first do a comparison betweenkand the elements nXkisn't necessarily inX. You might want to clarify it in your question.