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

Robert Kern robert.kern at gmail.com
Mon Mar 2 01:58:27 EST 2009


On Mon, Mar 2, 2009 at 00:50, Stephen Simmons <mail at 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