On Fri, Feb 29, 2008 at 11:12 PM, Charles R Harris < [EMAIL PROTECTED]> wrote:
> > > On Fri, Feb 29, 2008 at 10:53 AM, John Hunter <[EMAIL PROTECTED]> 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 > Oops, ind = arange(len(x)). I suppose nonzero would work as well. Chuck
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion