Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Symbolic cumulative sum

`B = cumsum(A)`

`B = cumsum(A,dim)`

`B = cumsum(___,direction)`

Create a vector and find the cumulative sum of its elements.

V = 1./factorial(sym([1:5])) sum_V = cumsum(V)

V = [ 1, 1/2, 1/6, 1/24, 1/120] sum_V = [ 1, 3/2, 5/3, 41/24, 103/60]

Create matrix a 4-by-4 symbolic matrix `A`

all
elements of which equal 1.

A = sym(ones(4,4))

A = [ 1, 1, 1, 1] [ 1, 1, 1, 1] [ 1, 1, 1, 1] [ 1, 1, 1, 1]

Compute the cumulative sum of elements of `A`

.
By default, `cumsum`

returns the cumulative sum
of each column.

sumA = cumsum(A)

sumA = [ 1, 1, 1, 1] [ 2, 2, 2, 2] [ 3, 3, 3, 3] [ 4, 4, 4, 4]

Create matrix a 4-by-4 symbolic matrix `A`

all
elements of which equal 1.

A = sym(ones(4,4))

A = [ 1, 1, 1, 1] [ 1, 1, 1, 1] [ 1, 1, 1, 1] [ 1, 1, 1, 1]

Compute the cumulative sum of each row of the matrix `A`

.

sumA = cumsum(A,2)

sumA = [ 1, 2, 3, 4] [ 1, 2, 3, 4] [ 1, 2, 3, 4] [ 1, 2, 3, 4]

Create matrix a 4-by-4 symbolic matrix, all elements of which equal 1.

A = sym(ones(4,4))

A = [ 1, 1, 1, 1] [ 1, 1, 1, 1] [ 1, 1, 1, 1] [ 1, 1, 1, 1]

Calculate the cumulative sum along the columns in both directions.
Specify the `'reverse'`

option to work from right
to left in each row.

columnsDirect = cumsum(A) columnsReverse = cumsum(A,'reverse')

columnsDirect = [ 1, 1, 1, 1] [ 2, 2, 2, 2] [ 3, 3, 3, 3] [ 4, 4, 4, 4] columnsReverse = [ 4, 4, 4, 4] [ 3, 3, 3, 3] [ 2, 2, 2, 2] [ 1, 1, 1, 1]

Calculate the cumulative sum along the rows in both directions.
Specify the `'reverse'`

option to work from right
to left in each row.

rowsDirect = cumsum(A,2) rowsReverse = cumsum(A,2,'reverse')

rowsDirect = [ 1, 2, 3, 4] [ 1, 2, 3, 4] [ 1, 2, 3, 4] [ 1, 2, 3, 4] rowsReverse = [ 4, 3, 2, 1] [ 4, 3, 2, 1] [ 4, 3, 2, 1] [ 4, 3, 2, 1]