Matlab

How to Calculate Moving Sum in MATLAB – A Basic Guide

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:

Moving_sum(t) = Data(t)+Data(t−1)+Data(t−2)+....+Data(t−(n+1))

 
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 built-in 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,k)
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.

v = rand(1, 5)
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.

A = magic(3)
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() built-in 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.

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.