[Numpy-discussion] reverse cumsum?

Jonathan Stickel jjstickel at vcn.com
Tue Jul 6 15:25:12 EDT 2010


On 7/6/10 10:42 , numpy-discussion-request at scipy.org wrote:
> Date: Tue, 06 Jul 2010 10:02:57 -0400
> From: Alan G Isaac<aisaac at american.edu>
> Subject: Re: [Numpy-discussion] reverse cumsum?
> To: Discussion of Numerical Python<numpy-discussion at scipy.org>
> Message-ID:<4C333791.5010505 at american.edu>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 7/6/2010 9:56 AM, Ken Basye wrote:
>> >  Is there a simple way to get a cumsum in reverse order?
>>>> >>>  x = np.arange(10)
>>>> >>>  x[::-1].cumsum()[::-1]
> array([45, 45, 44, 42, 39, 35, 30, 24, 17,  9])
>
> Is that what you want?
>
> Alan Isaac

Or, you can do:

In [1]: a = np.arange(10)
In [5]: np.sum(a) - np.cumsum(a)
Out[5]: array([45, 44, 42, 39, 35, 30, 24, 17,  9,  0])

Jonathan



More information about the NumPy-Discussion mailing list