[Numpy-discussion] Want cumsum-like function

Robert Kern robert.kern@gmail....
Wed Feb 24 23:11:28 CST 2010


On Wed, Feb 24, 2010 at 23:03, Peter Shinners <pete@shinners.org> wrote:
> On 02/24/2010 09:00 PM, Robert Kern wrote:
>> On Wed, Feb 24, 2010 at 22:53, Peter Shinners<pete@shinners.org>  wrote:
>>
>>> I want a function that works like cumsum, but starts at zero, instead of
>>> starting with the first actual value.
>>>
>>> For example; I have an array with [4,3,3,1].
>>> Cumsum will give me an array with [4,7,10,11].
>>> I want an array that is like [0,4,7,8].
>>>
>> I don't understand what process would give you [0, 4, 7, 8] rather
>> than [0, 4, 7, 10]. Can you explain a bit more?
>>
> Just brain failure. Yes, [0,4,7,10] is the desired result.

np.cumsum(np.hstack(0, initial_array[:-1]))

Correct, but not necessarily the most efficient.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list