How to Calculate Moving Sum in MATLAB?


In this article, we will explore how to calculate moving sum using MATLAB programming. But before that let us have a look into the concept of moving sum.

What is Moving Sum?

Moving sum is a mathematical operation that allows to calculate the sum of a set of numbers over a specified interval, known as window size or rolling window. It is also known as cumulative sum or rolling sum. Moving sum is widely used in the field of data analysis and signal processing.

Moving sum is generally performed on sequential data where the order of data elements is significant like in case of time series data. Moving sum allows us to understand the patterns and trends in the data.

In mathematics, we can use the following formula to calculate the moving sum at each time step,

Moving Sum(t) = Data(t)+Data(t−1)+Data(t−2)+....+Data(t−(n+1))

Here, Data(t) is the data at time t and n is the window size.

In this formula, it can be seen that when the rolling window moves through the data, a new value is obtained in each step.

The moving sum is widely used in various applications such as signal processing, economics, finance, and more.

Let us now discuss how to calculate the moving sum using MATLAB.

Moving Sum using MATLAB

MATLAB provides a built−in function named ‘movsum’ to calculate the moving of data elements of an array over a particular interval. Based on requirements of data analysis, the ‘movsum’ function can have different syntaxes as follows:

  • S = movsum(A, x);

  • S = movsum(A, [xl xr]);

  • S = movsum(A, x, dim);

  • S = movsum(A, x, nanflag);

Now, let us discuss each syntax of the ‘movsum’ function with the help of MATLAB programs.

S = movsum(X, k)

This syntax of the ‘movsum’ function is used to calculate the moving sum of elements of an array ‘X’ using a rolling window of size ‘a’. In this case, the output stored in the ‘S’ variable is an array of the same size as the array ‘A’.

In the output array ‘S’, each element will be a sum of the current element of the array ‘A’ and the ‘(x−1)’ preceding elements.

The following MATLAB program demonstrates the implementation of this syntax.

Example

% MATLAB code for calculating moving sum with a specified window size
% Define an input vector and window size
A = [2, 4, 6, 8, 10, 12];
x = 4;
% Calculate the moving sum
S = movsum(A, x);
% Display the input vector and moving sum
disp('The input vector is:');
disp(A);
disp('The moving sum is:');
disp(S);

Output

The input vector is:
    2    4    6    8   10   12
The moving sum is:
    6   12   20   28   36   30

Code Explanation

In this MATLAB program, firstly, we define an input vector ‘A’ and a window size ‘x’. Then, we perform the moving sum on the vector ‘A’ using the ‘movsum’ function with the specified window size ‘x’. Finally, we display the input vector and the moving sum vector by using the ‘disp’ function.

S = movsum(A, [xl xr])

This syntax of the ‘movsum’ function is used to calculate the moving sum of a vector with different window sizes. In this case, the ‘xl’ elements are used to calculate the moving sum of left side elements and the ‘xr’ elements are used to calculate the moving sum of right−side elements. This syntax also gives an output array ‘S’ of the same size as the array ‘A’.

The following MATLAB program demonstrates the implementation of this syntax of the ‘movsum’ function.

Example

% MATLAB code for calculating moving sum with different window sizes
% Define an input vector and different window sizes for left and right sides
A = [2, 4, 6, 8, 10, 12];
xl = 3;		% Left side elements
xr = 2;		% Right side elements
% Calculate the moving sum
S = movsum(A, [xl xr]);
% Display the input vector and moving sum
disp('The input vector is:');
disp(A);
disp('The moving sum is:');
disp(S);

Output

The input vector is:
    2    4    6    8   10   12
The moving sum is:
   12   20   30   42   40   36

Code Explanation

In this MATLAB program, we start by defining an input vector ‘A’ and the two window sizes ‘xl’ and ‘xr’ for left side and right−side elements respectively. Then, we perform the moving sum on the vector ‘A’ using the ‘movsum’ function with the specified window sizes ‘xl’ and ‘xr’. Finally, we display the input vector and the moving sum vector by using the ‘disp’ function.

S = movsum(A, x, dim)

This syntax of the ‘movsum’ function is used to calculate the sum of an array ‘A’ along a specified dimension, denoted by ‘dim’. We need to specify a window size ‘x’ as well.

Here, the value of ‘dim’ parameter determines the dimension to calculate the moving sum. Where, if ‘dim = 1’, then the moving sum will be calculated along the columns of the array, and if ‘dim = 2’, the moving sum is calculated along the rows of the array.

The following MATLAB program demonstrates the use of this syntax to compute the moving sum.

Example

% MATLAB code for calculating moving sum along specified dimension
% Define an input array
A = [2, 4, 6; 8, 10, 12; 5, 9, 7];
% Calculate the moving sum along the columns
S1 = movsum(A, 3, 1);
% Calculate the moving sum along the rows
S2 = movsum(A, 3, 2);
% Display the input vector and moving sums
disp('The input vector is:');
disp(A);
disp('The moving sum along columns is:');
disp(S1);
disp('The moving sum along rows is:');
disp(S2);

Output

The input array is:
    2    4    6
    8   10   12
    5    9    7
The moving sum along columns is:
   10   14   18
   15   23   25
   13   19   19
The moving sum along rows is:
    6   12   10
   18   30   22
   14   21   16

Code Explanation

In this MATLAB program, we start by defining an input array ‘A’ Then, we use the ‘movsum’ function to perform the moving sum on the array ‘A’ along the columns (dim = 1) and rows (dim = 2) respectively and store the results in variables ‘S1’ and ‘S2’ respectively. Finally, we display the input array and the moving sum arrays by using the ‘disp’ function.

S = movsum(A, x, nanflag)

This syntax of the ‘movsum’ function allows us to perform moving sum of a vector or array containing NaN values.

We can specify ‘includenan’ or ‘omitnan’ as the nanflag parameter. Where, the ‘includenan’ perform the moving sum by including NaN values and reflect them in the result as NaN. On the other hand, the ‘omitnan’ option allows to ignore the NaN values and perform the moving sum based on the non−Nan values.

Note − Number + NaN = NaN

The following MATLAB program demonstrates the implementation of this syntax of the ‘movsum’ function.

Example

% MATLAB code for calculating moving sum by handling NaN elements
% Define an input vector with NaN elements
A = [2, 4, NaN, 8, NaN, 12];
% Calculate the moving sum with include NaN elements
S1 = movsum(A, 3, 'includenan');
% Calculate the moving sum with omitting NaN elements
S2 = movsum(A, 3, 'omitnan');
% Display the input vector and moving sums
disp('The input vector is:');
disp(A);
disp('The moving sum with including NaN values is:');
disp(S1);
disp('The moving sum with omitting NaN values is:');
disp(S2);

Output

The input vector is:
     2     4   NaN     8   NaN    12

The moving sum with including NaN values is:
     6   NaN   NaN   NaN   NaN   NaN

The moving sum with omitting NaN values is:
     6     6    12     8    20    12

Code Explanation

In this MATLAB program, we start by defining an input vector ‘A’ Then, we use the ‘movsum’ function to perform the moving sum on the array ‘A’ with including NaN elements (i.e. nanflag = ‘includenan’) and with omitting NaN elements (i.e. nanflag = ‘omitnan’) respectively and store the results in variables ‘S1’ and ‘S2’ respectively. Finally, we display the input vector and the moving sum vectors by using the ‘disp’ function.

Conclusion

In conclusion, the moving sum is a mathematical operation that allows to calculate the cumulative sum of arrays or vectors and is commonly used in data analysis and signal processing applications. MATLAB provides a built-in function namely ‘movsum’ which can be used to perform the moving sum. It can have different syntaxes based on the use case. We have explained all these syntaxes of the ‘movsum’ function in the above sections of this article along with sum example MATLAB programs to explain their implementation.

Updated on: 07-Aug-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements