A common numerical operation is cumulative summing.
The cumsum() operation has the output array of the same shape as the input array, with each element as the sum of all the previous elements in that dimension.
operate quite similarly.
When translating code between Matlab and Python, as always keep in mind Matlab’s one-based indexing vs. Python’s zero-based indexing.
That is, when using cumsum() over an axis, be sure to select the correct axis–Matlab
cumsum(..., 1) is equivalent to
These examples are equivalent. Suppose input:
x = [2, 3, 5, 8]
y = cumsum(x) y = 2 5 10 18
y = numpy.cumsum(x) >>> y array([ 2, 5, 10, 18])