[Numpy-discussion] contiguous true

Charles R Harris charlesr.harris@gmail....
Sat Mar 1 00:12:56 CST 2008


On Fri, Feb 29, 2008 at 10:53 AM, John Hunter <jdh2358@gmail.com> wrote:

> [apologies if this is a resend, my mail just flaked out]
>
> 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
>
> In [101]: x = np.random.rand(20)>.4
>
> In [102]: x
> Out[102]:
> array([False,  True,  True, False, False,  True,  True, False, False,
>        True, False,  True, False,  True,  True,  True, False,  True,
>       False,  True], dtype=bool)
>
> I would like to find ind=1 for N=2 and ind=13 for N=2.  I assume with
> the right cumsum, diff and maybe repeat magic, this can be vectorized,
> but the proper incantation is escaping me.
>
> for N==3, I thought of
>
> In [110]: x = x.astype(int)
> In [112]: y = x[:-2] + x[1:-1] + x[2:]
>
> In [125]: ind = (y==3).nonzero()[0]
>
> In [126]: if len(ind): ind = ind[0]
>
> In [128]: ind
> Out[128]: 13
>


This may be more involved than you want, but

In [37]: prng = random.RandomState(1234567890)

In [38]: x = prng.random_sample(50) < 0.5

In [39]: y1 = concatenate(([False], x[:-1]))

In [40]: y2 = concatenate((x[1:], [False]))

In [41]: beg = ind[x & ~y1]

In [42]: end = ind[x & ~y2]

In [43]: cnt = end - beg + 1

In [44]: i = beg[cnt == 4]

In [45]: i
Out[45]: array([28])

In [46]: x
Out[46]:
array([False, False, False, False,  True, False,  True, False, False,
       False,  True, False,  True, False,  True,  True,  True,  True,
        True, False, False, False,  True, False,  True, False, False,
       False,  True,  True,  True,  True, False, False,  True, False,
       False, False, False, False, False, False, False,  True, False,
       False,  True, False,  True, False], dtype=bool)

produces a list of the indices where sequences of length 4 begin.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080229/265cf0f2/attachment.html 


More information about the Numpy-discussion mailing list