Finding the moving sum or running sum of a set or interval is a complicated and time taking mathematical problem when we solve it manually. Also, when we deal with large data sets, it becomes impractical to manually calculate the moving sum. But, now with MATLAB, it has become an easy task using the movsum() function.
If you are not familiar with how to calculate moving sum in MATLAB, follow this guide.
What is a Moving Sum?
The moving sum is a mathematical operation used for computing the sum of the elements of a set over a specified interval that is called window size or rolling window. This sum can be used in data mining and signal processing. Usually, this sum is applied to the sequential data so this sum can also be used to identify the data pattern or trend.
In terms of mathematics, the moving sum can be defined as:
Here:
Data(t) represents data at time t and n represents the window size.
How to Calculate Moving Sum in MATLAB?
We can easily and efficiently compute the moving sum in MATLAB using the builtin movsum() function that enables us to find the moving sum of the elements of a data set over the specified range. This function accepts a data set and the window size as arguments and returns the computed moving sum.
Syntax
The movsum() function’s syntax is given below:
M = movsum(A,[kb kf])
M = movsum(___,dim)
Here:
The function M = movsum(A,k) computes the moving sum over the window size k. If we have k as an odd number, the window will be created across the current position of the elements. If k is an even number, the window will be created across the current and previous elements. The window size k is automatically truncated over the endpoints of the data sets. It automatically sets its size according to the last remaining endpoints.

 If A represents a vector, the function will operate along the length of A.
 If A is a multidirectional array, the function will work along its dimension that is not equal to 1.
The function M = movsum(A,[kb kf]) computes the moving sum having the window length kb+kf+1 including the elements of the current position, kb backward elements, and kf forward elements.
The function M = movsum(___,dim) computes the moving sum along the given dimension dim for any given above syntax.
Examples
Consider some examples to understand the implementation of moving sum in MATLAB.
Example 1: How to Find the Moving Sum of a Vector Using the movsum() Function in MATLAB?
In the given MATLAB code, we compute the moving sum of a vector using the movsum() function with window size k=4.
mov_sum = movsum(v,4)
Example 2: How to Find the Moving Sum of a Multidirectional Array Using the movsum() Function in MATLAB?
This MATLAB code finds the moving sum of the given matrix A along dimension 2 having window size [3, 0] using the movsum() function.
mov_sum = movsum(A,[3 0], 2)
Conclusion
Finding the moving sum of a data set over a specified interval is a very common task mostly adopted by scientists and engineers in signal processing and data mining. This task can become complicated while dealing with large data sets. MATLAB makes it easy by providing the movsum() builtin function. This function allows us to compute the moving sum of any large or small data set for the given window size. This guide explored how to find the moving sum using movsum() in MATLAB.