Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find stretches of Trues in numpy array

Is there a good way to find stretches of Trues in a numpy boolean array? If I have an array like:

x = numpy.array([True,True,False,True,True,False,False])

Can I get an array of indices like:

starts = [0,3]
ends = [1,4]

or any other appropriate way to store this information. I know this can be done with some complicated while loops, but I'm looking for a better way.

like image 858
user3266890 Avatar asked Apr 24 '15 16:04

user3266890


1 Answers

You can pad x with Falses (one at the beginning and one at the end), and use np.diff. A "diff" of 1 means transition from False to True, and of -1 means transition from True to False.

The convention is to represent range's end as the index one after the last. This example complies with the convention (you can easily use ends-1 instead of ends to get the array in your question):

x1 = np.hstack([ [False], x, [False] ])  # padding
d = np.diff(x1.astype(int))
starts = np.where(d == 1)[0]
ends = np.where(d == -1)[0]
starts, ends
=> (array([0, 3]), array([2, 5]))
like image 196
shx2 Avatar answered Sep 24 '22 07:09

shx2