Does anyone (the usual case) know a faster than linear way to find the endpoints of the Boolean property of an array.
For example, numpy.nonzero (a) [0] [- 1] is the index of the last nonzero element a (dimension = 0) and, like numpy.nonzero (a) [0] [0] is the index of the first nonzero element.
If we know that we only care about the first or last element, we can use less memory and have better run time in normal mode than doing non-zero, as described above. For example, if we adhere to a linear search, we can at least start from the corresponding end (look back to find the last value that matches the condition). Or we can use a binary search (for example, if the middle element matches the condition, we do not need to check the 1st half to find the last element where it is true). It seems common enough that an existing implementation may exist, but I have not found anything like it.
argmax.
argmax
a = np.array([False, False, True, True, True]) first_True = a.argmax() last_True = len(a) - 1 - a[::-1].argmax()
argmin False, , , a. , , , a "" , , False, True. , False true:
argmin
a
first_True = a.searchsorted(True, 'left')