[Numpy-discussion] Array accumulation in numpy
Tue Feb 19 09:16:54 CST 2013
On Tue, 2013-02-19 at 10:00 -0500, Tony Ladd wrote:
> I want to accumulate elements of a vector (x) to an array (f) based on
> an index list (ind).
> For example:
> What I want would be produced by the loop
> for i=range(6):
> The answer is f=array([ 0., 7., 0., 6., 5., 0., 0., 0., 0., 3.])
> When I try to use implicit arguments
> I get f=array([ 0., 6., 0., 4., 5., 0., 0., 0., 0., 3.])
> So it takes the last value of x that is pointed to by ind and adds it to
> f, but its the wrong answer when there are repeats of the same entry in
> ind (e.g. 3 or 1)
> I realize my code is incorrect, but is there a way to make numpy
> accumulate without using loops? I would have thought so but I cannot
> find anything in the documentation.
You might be interested in this:
But anyway, you should however be able to do what you want to do using
np.bincount with the weights keyword argument.
> Would much appreciate any help - probably a really simple question.
More information about the NumPy-Discussion