Matlab

How to Find Cumulative Sum Using cumsum() in MATLAB?

Finding the partial sum or cumulative sum of a sequence or an array is a complicated and time taking problem when we solve it manually. Also, when we deal with large sequences and arrays, it becomes impossible to manually calculate the cumulative sum. But, with MATLAB, it has become an easy task by using its built-in cumsum() function

If you are not familiar with the working of the cumsum() function, this blog will teach you about the working of this function in finding the cumulative sum in MATLAB

What is a Cumulative Sum?

The cumulative sum or partial sum of a sequence or an array is a running sum that can be defined as the sum of elements up to a given point:

If we have a sequence having elements {a,b,c,d….N), it has the cumulative sum as:

a,
a+b,
a+b+c…,
…….
a+b+c+d+…+N

The cumulative sum of a sequence can be used to calculate the total sum of the sequence, or to find the sum of the elements up to a certain point, or it can also be used to find the average of the sequence.

How to Use the cumsum() MATLAB’s Function?

The cumsum() is a built-in function in MATLAB that enables us to find the cumulative sum of the given sequence or array. This function accepts a sequence or an array as an argument and computes its cumulative sum.

Note: The cumulative sum of an array has the same class as the array except for the logical array whose cumulative sum has the class double.

Syntax

The cumsum() used different syntaxes in MATLAB, as given below and each has its own uses:

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

Here,

The function B = cumsum(A) computes the cumulative sum beginning from the first dimension of A.

  • If A is a vector, B will also be a vector containing the same length as A having the cumulative sum of A.
  • If A is a matrix, B will also be a matrix having the same size as A containing the cumulative sum of A in each column.
  • If A is a multidirectional array, B will also be an array having the same size as A containing the cumulative sum of A starting from the first dimension of A.
  • If A is a timetable or table, B will also be a timetable or table having the same size as A containing the cumulative sum in each variable.

Function B = cumsum(A, dim) computes the cumulative sum along the given dimension dim.

Function B = computes cumsum(___, direction) defines the direction for calculating the cumulative sum. For example, if we specify direction = ‘reverse’, the cumulative sum from any previous syntax will be calculated from the end.

Examples

Consider some examples to understand the implementation of the cumsum() function in MATLAB.

Example 1: How to Find the Cumulative Sum of a Vector Using the cumsum() Function in MATLAB?

In this MATLAB code, we compute the cumulative sum of a vector using the cumsum() function in MATLAB.

v = rand(1, 5)
cum_sum = cumsum(v)

Example 2: How to Find the Cumulative Sum of a Matrix Using the cumsum() Function in MATLAB?

This MATLAB code finds the cumulative sum of the given matrix A along dimension dim = 2 using the cumsum() function.

A = magic(3)
cum_sum = cumsum(A, 2)

Example 3: How to Find the Cumulative Sum of an Array Using the cumsum() Function in MATLAB?

The given example uses the cumsum() function with a reverse formula for computing the cumulative sum of the given array in MATLAB.

A = rand(2,3, 2)
cum_sum = cumsum(A, 2, "reverse")

Conclusion

Finding the cumulative sum of a sequence or an array is a very common task mostly adopted by scientists and engineers. This task can become complicated while dealing with large data sets. MATLAB makes it easy by providing the cumsum() built-in function. This function allows us to compute the cumulative sum of any large or small data set. This guide has discovered how to compute the cumulative sum using cumsum() in MATLAB. The given examples in this guide will assist you find the cumulative sum for vectors, matrices, and arrays.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.