[Numpy-discussion] summing an array
Bob Dowling
rjd4+numpy at cam.ac.uk
Thu Aug 18 10:58:25 EDT 2011
On 18/08/11 15:19, Chris Withers wrote:
> Hopefully a simple newbie question, if I have an array such as :
>
> array([0, 1, 2, 3, 4])
>
> ...what's the best way to cummulatively sum it so that I end up with:
>
> array([0, 1, 3, 6, 10])
>
> How would I do this both in-place and to create a new array?
>>> a = numpy.arange(0,5)
>>> a
array([0, 1, 2, 3, 4])
>>> numpy.add.accumulate(a)
array([ 0, 1, 3, 6, 10])
>>> numpy.add.accumulate(a, out=a)
array([ 0, 1, 3, 6, 10])
>>> a
array([ 0, 1, 3, 6, 10])
>>>
And similarly with numpy.multiply for products etc.
More information about the NumPy-Discussion
mailing list