Main Content

cumsum

Symbolic cumulative sum

Description

example

B= cumsum(A)returns the cumulative sum ofAstarting at the beginning of the first array dimension inAwhose size does not equal 1. The outputBhas the same size asA.

  • IfAis a vector, thencumsum(A)returns a vector containing the cumulative sum of the elements ofA.

  • IfAis a matrix, thencumsum(A)returns a matrix containing the cumulative sums of each column ofA.

  • IfAis a multidimensional array, thencumsum(A)acts along thefirst nonsingleton dimension.

example

B= cumsum(A,dim)returns the cumulative sum along dimensiondim. For example, ifAis a matrix, thencumsum(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 ofAby working from end to beginning of the second dimension.

example

B= cumsum(___,nanflag)specifies whether to include or omitNaNvalues from the calculation for any of the previous syntaxes.cumsum(A,'includenan')includes allNaN在计算值cumsum(A,'omitnan')ignores them.

Examples

collapse all

Create a symbolic vector. Find the cumulative sum of its elements.

symsxA = (1:5)*x
A =
                       
                        
                         
                          
                           (
                          
                           
                            
                             
                              
                               x
                             
                            
                            
                             
                              
                               
                                
                                 2
                                
                                
                                
                                 x
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 3
                                
                                
                                
                                 x
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 4
                                
                                
                                
                                 x
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 5
                                
                                
                                
                                 x
                               
                              
                             
                            
                           
                          
                          
                           )
                         
                        
                       

In the vector of cumulative sums, elementB(2)is the sum ofA(1)andA(2), whileB(5)is the sum of elementsA(1)throughA(5).

B = cumsum(A)
B =
                       
                        
                         
                          
                           (
                          
                           
                            
                             
                              
                               x
                             
                            
                            
                             
                              
                               
                                
                                 3
                                
                                
                                
                                 x
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 6
                                
                                
                                
                                 x
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 10
                                
                                
                                
                                 x
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 15
                                
                                
                                
                                 x
                               
                              
                             
                            
                           
                          
                          
                           )
                         
                        
                       

Create a 3-by-3 symbolic matrixAwhose elements are all equal to 1.

A = sym(ones(3))
A =

( 1 1 1 1 1 1 1 1 1 )

Compute the cumulative sum of elements ofA. By default,cumsumreturns the cumulative sum of each column.

B = cumsum(A)
B =

( 1 1 1 2 2 2 3 3 3 )

To compute the cumulative sum of each row, set the value of thedimoption to2.

B = cumsum(A,2)
B =

( 1 2 3 1 2 3 1 2 3 )

Create a 3-by-3-by-2 symbolic array.

symsxyA(:,:,1) = [x y 3; 3 x y; y 2 x]; A(:,:,2) = [x y 1/3; 1 y x; 1/3 x 2]; A
A(:,:,1) =

( x y 3 3 x y y 2 x )

A(:,:,2) =

( x y 1 3 1 y x 1 3 x 2 )

Compute the cumulative sum along the rows by specifyingdimas2. Specify the“反向”选择工作right to left in each row. The result is the same size asA.

B = cumsum(A,2,“反向”)
B(:,:,1) =

( x + y + 3 y + 3 3 x + y + 3 x + y y x + y + 2 x + 2 x )

B(:,:,2) =

( x + y + 1 3 y + 1 3 1 3 x + y + 1 x + y x x + 7 3 x + 2 2 )

To compute the cumulative sum along the third (page) dimension, specifydimas3. Specify the“反向”选择工作largest page index to smallest page index.

B = cumsum(A,3,“反向”)
B(:,:,1) =

( 2 x 2 y 10 3 4 x + y x + y y + 1 3 x + 2 x + 2 )

B(:,:,2) =

( x y 1 3 1 y x 1 3 x 2 )

Create a symbolic vector containingNaNvalues. Compute the cumulative sums.

A = [sym('a') sym('b') 1 NaN 2]
A =
                       
                        
                         
                          
                           (
                          
                           
                            
                             
                              
                               a
                             
                            
                            
                             
                              
                               b
                             
                            
                            
                             
                              
                               1
                             
                            
                            
                             
                              
                               NaN
                             
                            
                            
                             
                              
                               2
                             
                            
                           
                          
                          
                           )
                         
                        
                       
B = cumsum(A)
B =
                       
                        
                         
                          
                           (
                          
                           
                            
                             
                              
                               a
                             
                            
                            
                             
                              
                               
                                
                                 a
                                
                                 +
                                
                                 b
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 a
                                
                                 +
                                
                                 b
                                
                                 +
                                
                                 1
                               
                              
                             
                            
                            
                             
                              
                               NaN
                             
                            
                            
                             
                              
                               NaN
                             
                            
                           
                          
                          
                           )
                         
                        
                       

You can ignoreNaNvalues in the cumulative sum calculation using the'omitnan'option.

B = cumsum(A,'omitnan')
B =
                       
                        
                         
                          
                           (
                          
                           
                            
                             
                              
                               a
                             
                            
                            
                             
                              
                               
                                
                                 a
                                
                                 +
                                
                                 b
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 a
                                
                                 +
                                
                                 b
                                
                                 +
                                
                                 1
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 a
                                
                                 +
                                
                                 b
                                
                                 +
                                
                                 1
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 a
                                
                                 +
                                
                                 b
                                
                                 +
                                
                                 3
                               
                              
                             
                            
                           
                          
                          
                           )
                         
                        
                       

Input Arguments

collapse all

Input array, specified as a symbolic vector, matrix, or multidimensional array.

Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.

Consider a two-dimensional input array,A:

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

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

cumsumreturnsAifdimis greater thanndims(A).

Direction of cumulation, specified as'forward'(default) or“反向”.

  • 'forward'works from1toendof the active dimension.

  • “反向”works fromendto1of the active dimension.

Data Types:char

NaNcondition, specified as:

  • 'includenan'— IncludeNaNvalues from the input when computing the cumulative sums, resulting inNaNvalues in the output.

  • 'omitnan'— Ignore allNaNvalues in the input. The sum of elements containingNaNvalues is the sum of all non-NaN元素。如果所有elements areNaN, thencumsumreturns 0.

Data Types:char

Output Arguments

collapse all

Cumulative sum array, returned as a vector, matrix, or multidimensional array of the same size as the inputA.

More About

collapse all

First Nonsingleton Dimension

The first nonsingleton dimension is the first dimension of an array whose size is not equal to 1.

For example:

  • IfXis a 1-by-n row vector, then the second dimension is the first nonsingleton dimension ofX.

  • IfXis a 1-by-0-by-n empty array, then the second dimension is the first nonsingleton dimension ofX.

  • IfXis a 1-by-1-by-3 array, then the third dimension is the first nonsingleton dimension ofX.

Version History

Introduced in R2013b