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

Ravi lists_ravi@lavabit....
Mon Mar 2 09:34:24 CST 2009


On Monday 02 March 2009 01:58:27 Robert Kern wrote:
> > for i in range(len(vals)):
> >    flattened[idx[i]]+=vals[i]
>
> flattened[idx] = vals

Assuming 'idx' and 'vals' are one-dimensional arrays, that should be
 flattened[ idx[:numpy.size(vals)] ] += vals
or
 flattened[ idx ] += vals
if 'vals' and 'idx' have the same size.

Regards,
Ravi




More information about the Numpy-discussion mailing list