Search code examples
pythonarraysnumpycumsum

Perform a reverse cumulative sum on a numpy array


Can anyone recommend a way to do a reverse cumulative sum on a numpy array?

Where 'reverse cumulative sum' is defined as below (I welcome any corrections on the name for this procedure):

if

x = np.array([0,1,2,3,4])

then

np.cumsum(x)

gives

array([0,1,3,6,10])

However, I would like to get

array([10,10,9,7,4]

Can anyone suggest a way to do this?


Solution

  • This does it:

    np.cumsum(x[::-1])[::-1]