[Numpy-discussion] contiguous true
John Hunter
jdh2358@gmail....
Fri Feb 29 11:53:09 CST 2008
[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
Thanks,
JDH
More information about the Numpy-discussion
mailing list