I have a (numpy) array representing a measurement curve. I am looking for the first index i
following which the subsequent N
elements satisfy some condition, e.g. lie within specific bounds. In pseudo code words I am looking for the minimal i
such that
lower_bound < measurement[i:i+N] < higher_bound
is satisfied for all elements in the range.
Of course I could do the following:
for i in xrange(len(measurement) - N):
test_vals = measurement[i:i + N]
if all([True if lower_bound < x < higher_bound else False for x in test_vals]):
return i
This is extremely inefficent as I am always comparing N
values for every i
.
What is the most pythonic way to achieve this? Has Numpy some built-in functionalities to find this?
EDIT: As per request I provide some example input data
a = [1,2,3,4,5,5,6,7,8,5,4,5]
lower_bound = 3.5
upper_bound = 5.5
N = 3
should return 3
as starting at a[3]
the elements are within the bounds for at least 3 values.