Reputation: 381
Given a numpy array a=[3,5,7]
How can I efficiently generate a second array b, where b[i] = numpy.Sum(a[0:i]?
I've looked through the numpy docs but the solution isn't jumping out at me...
The expected output would be b=[3,8,15]
Any ideas will be gratefully received!!!
Thanks,
Doug
Upvotes: 0
Views: 38
Reputation: 2468
You seem to want the cumsum
function from numpy here:
a=np.array([3,5,7])
In [1]: np.cumsum(a)
Out[1]: array([ 3, 8, 15], dtype=int32)
Upvotes: 2