[Numpy-discussion] contiguous true

Anne Archibald peridot.faceted@gmail....
Sat Mar 1 00:56:41 CST 2008


On 01/03/2008, Charles R Harris <charlesr.harris@gmail.com> wrote:

> > On Fri, Feb 29, 2008 at 10:53 AM, John Hunter <jdh2358@gmail.com> wrote:
> > > I have a boolean array and would like to find the lowest index "ind"
> > > where N contiguous elements are all True.  Eg, if x is

[...]

> Oops, ind = arange(len(x)). I suppose nonzero would work as well.

I'm guessing you're alluding to the fact that diff(nonzero(x)) gives
you a list of the run lengths of Falses in x (except possibly for the
first one).

If you have a fondness for the baroque, you can try

numpy.where(numpy.convolve(x,[1,]*N,'valid')==N)

For large N this can even use Fourier-domain convolution (though you'd
then have to be careful about round-off error).

Silly, really, it's O(NM) or O(N log M) instead of O(N).

Anne


More information about the Numpy-discussion mailing list