[Numpy-discussion] efficient way to do this?

John Hunter jdh2358 at gmail.com
Mon Sep 22 10:41:30 EDT 2008


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.

In the real use case, there will be significant auto-correlation among
the places where the condition is satisfied.  Eg, if it is satisfied
at some index, it is likely that it will be satisfied for many of its
neighbors.  Eg, the real case looks more like

    y = np.sin(2*np.pi*np.linspace(0, 2, N))

    ind = np.nonzero(y>0.95)[0]
    marked2 = np.zeros(N, bool)
    for i in ind:
        marked2[i:i+Nmark] = True

Thanks in advance for any hints,
JDH



More information about the NumPy-Discussion mailing list