Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

cumsum

Symbolic cumulative sum

Syntax

Description

example

B = cumsum(A) returns an array the same size as A containing the cumulative sum.

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

  • If A is a matrix, then cumsum(A) returns a matrix containing the cumulative sums of each column of A.

example

B = cumsum(A,dim) returns the cumulative sum along dimension dim. For example, if A is a matrix, then cumsum(A,2) returns the cumulative sum of each row.

example

B = cumsum(___,direction) specifies the direction using any of the previous syntaxes. For instance, cumsum(A,2,'reverse') returns the cumulative sum within the rows of A by working from end to beginning of the second dimension.

Examples

Cumulative Sum of Vector

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]

Cumulative Sum of Each Column in Symbolic Matrix

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]

Cumulative Sum of Each Row in Symbolic Matrix

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]

Reverse Cumulative Sum

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]

Input Arguments

collapse all

Input array, specified as a vector or matrix.

Dimension to operate along, specified as a positive integer. The default value is 1.

Consider a two-dimensional input array, A:

  • cumsum(A,1) works on successive elements in the columns of A and returns the cumulative sum of each column.

  • cumsum(A,2) works on successive elements in the rows of A and returns the cumulative sum of each row.

cumsum returns A if dim is greater than ndims(A).

Direction of cumulation, specified as the 'forward' (default) or 'reverse'.

  • 'forward' works from 1 to end of the active dimension.

  • 'reverse' works from end to 1 of the active dimension.

Output Arguments

collapse all

Cumulative sum array, returned as a vector or matrix of the same size as the input A.

See Also

| | | |

Introduced in R2013b

Was this topic helpful?