[Numpy-discussion] efficient way to do this?

Robert Kern robert.kern@gmail....
Mon Sep 22 10:13:19 CDT 2008

On Mon, Sep 22, 2008 at 09:41, John Hunter <jdh2358@gmail.com> wrote:
> I have a an array of indices into a larger array where some condition
> is satisfied.  I want to create a larger set of indices which *mark*
> all the indicies following the condition over some Nmark length
> window.  In code:
>    import numpy as np
>    N = 1000
>    Nmark = 20
>    ind = np.nonzero(np.random.rand(N)<0.01)[0]
>    marked = np.zeros(N, bool)
>    for i in ind:
>        marked[i:i+Nmark] = True
> I am going to have to do this over many arrays, and so I want to do it
> efficiently.  Is there a way to do the above more efficiently, eg w/o
> the loop.

marked[ind + np.arange(Nmark)] = True

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