cumsum - Cumulative sum

Syntax

B = cumsum(A)
B = cumsum(A,dim)

Description

B = cumsum(A) returns the cumulative sum along different dimensions of an array.

If A is a vector, cumsum(A) returns a vector containing the cumulative sum of the elements of A.

If A is a matrix, cumsum(A) returns a matrix the same size as A containing the cumulative sums for each column of A.

If A is a multidimensional array, cumsum(A) works on the first nonsingleton dimension.

B = cumsum(A,dim) returns the cumulative sum of the elements along the dimension of A specified by scalar dim. For example, cumsum(A,1) works along the first dimension (the columns); cumsum(A,2) works along the second dimension (the rows).

Examples

cumsum(1:5)
ans = 
       [1  3  6  10  15]

A = [1 2 3; 4 5 6];

cumsum(A,1)
ans = 
       1     2     3
       5     7     9

cumsum(A,2)
ans = 
       1     3     6
       4     9    15

See Also

cumprod, prod, sum

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS