[Numpy-discussion] contiguous true
Robert Kern
robert.kern at gmail.com
Fri Feb 29 13:13:04 EST 2008
On Fri, Feb 29, 2008 at 11:53 AM, John Hunter <jdh2358 at 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 smallish N (< 100 perhaps), I'd do something like this:
In [57]: from numpy import *
In [58]: prng = random.RandomState(1234567890)
In [59]: x = prng.random_sample(50) < 0.5
In [60]: x
Out[60]:
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)
In [61]: N = 2
In [62]: mask = ones(len(x) - N + 1, dtype=bool)
In [63]: for i in range(N):
....: mask &= x[i:len(x)-N+1+i]
....:
....:
In [64]: mask
Out[64]:
array([False, False, False, False, False, False, False, False, False,
False, False, False, False, False, True, True, True, True,
False, False, False, False, False, False, False, False, False,
False, True, True, True, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False], dtype=bool)
In [65]: nonzero(mask)[0][0]
Out[65]: 14
In [66]: x[13:20]
Out[66]: array([False, True, True, True, True, True, False], dtype=bool)
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list