[Numpy-discussion] efficient way to do this?

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


On Mon, Sep 22, 2008 at 10:18, John Hunter <jdh2358@gmail.com> wrote:
> On Mon, Sep 22, 2008 at 10:13 AM, Robert Kern <robert.kern@gmail.com> wrote:
>
>> marked[ind + np.arange(Nmark)] = True
>
> That triggers a broadcasting error:
>
> Traceback (most recent call last):
>  File "/home/titan/johnh/test.py", line 13, in ?
>    marked3[ind + np.arange(Nmark)] = True
> ValueError: shape mismatch: objects cannot be broadcast to a single shape
>
> I am hoping there is some clever way to do this with broadcasting, I
> am just not that clever...

Oh, right. Sorry.

ind2mark = np.asarray((ind[:,np.newaxis] + np.arange(Nmark).flat).clip(0, N-1)
marked[ind2mark] = 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