[Numpy-discussion] Easy way to vectorize a loop?

Robert Kern robert.kern@gmail....
Mon Mar 2 00:58:27 CST 2009


On Mon, Mar 2, 2009 at 00:50, Stephen Simmons <mail@stevesimmons.com> wrote:
> Hi,
>
> Can anyone help me out with a simple way to vectorize this loop?
>
> # idx and vals are arrays with indexes and values used to update array data
> # data = numpy.ndarray(shape=(100,100,100,100), dtype='f4')
> flattened = data.ravel()
> for i in range(len(vals)):
>    flattened[idx[i]]+=vals[i]

flattened[idx] = vals

-- 
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