scotsman60
scotsman60

Reputation: 381

Using numpy, how can I generate an array where the value at each index is the sum of the values from 0 to that same index in a second array?

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

Answers (1)

user8408080
user8408080

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

Related Questions