[SciPy-user] indices of consecutive elements

Daniel Ashbrook anjiro@cc.gatech....
Tue Dec 2 10:43:34 CST 2008


I'm trying to figure out a way to return the indices of the start and
end of a run of consecutive elements that match some condition, but only
if there are more than a certain number.

For example, take the array (with indices in comment for clarity):

#0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22
[0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0]

I want to find the start and end indices of all runs of 1s with length
of 4 or longer; so here the answer would be:

[[2,5], [15,18]]

Is there a reasonable way to do this without looping? I've been playing
around with diff() and where() but without too much progress.

Thanks,


dan


More information about the SciPy-user mailing list