[Numpy-discussion] summing an array

Skipper Seabold jsseabold@gmail....
Thu Aug 18 09:22:51 CDT 2011


On Thu, Aug 18, 2011 at 10:19 AM, Chris Withers <chris@simplistix.co.uk> wrote:
> Hi All,
>
> 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?
>

[~/]
[1]: a = np.arange(5)

[~/]
[2]: a
[2]: array([0, 1, 2, 3, 4])

[~/]
[3]: np.cumsum(a)
[3]: array([ 0,  1,  3,  6, 10])

[~/]
[4]: np.cumsum(a,out=a)
[4]: array([ 0,  1,  3,  6, 10])

[~/]
[5]: a
[5]: array([ 0,  1,  3,  6, 10])

Skipper


More information about the NumPy-Discussion mailing list