[Numpy-discussion] Find contiguous sequences of booleans in arrays
stefan
stefan@sun.ac...
Mon Sep 17 06:15:07 CDT 2007
Hi Kurdt,
On Mon, 17 Sep 2007 12:18:51 +0200, "Kurdt Bane" <kurdt.bane@gmail.com>
wrote:
> I've got an 1-D array of bools and I'd like to find the length of the
> first
> contiguous sequence of True values, starting from position [0] of the
> array.
One way would be:
x = N.array([True,True,False,True])
x.argmin()
> And what if I want to
> get
> a list of all the contiguous sequences of True values, above a given
> threshold?
x = N.array([True,True,False,False,False,True,False,False])
jumps, = N.where(N.diff(x))
idx = N.concatenate(([0],jumps+1,[-1]))
print [x[a:b] for (a,b) in zip(idx[:-1],idx[1:]) if x[a]]
Cheers
Stéfan
More information about the Numpy-discussion
mailing list