moving sum using movsum()

21 views (last 30 days)
Shivaani S
Shivaani S on 24 May 2020
Commented: Shivaani S on 24 May 2020
I am new to matlab and this is a code for computing moving sum from 3 consecutive numbers in matlab from Mathworks documentation. I am unable to understand what they mean by discarding endpoint calculations in the explanation(underlined ). I do not understand the significance of endpoints and discard keyword in the given documentation.If someone can explain this it would be very helpful .
Compute the three-point centered moving sum of a row vector, but discard any calculation that uses fewer than three points from the output. In other words, return only the sums computed from a full three-element window, discarding endpoint calculations.
A = [4 8 6 -1 -2 -3 -1 3 4 5];
M = movsum(A,3,'Endpoints','discard')
M = 1×8
18 13 3 -6 -6 -1 6 12

Accepted Answer

Ameer Hamza
Ameer Hamza on 24 May 2020
If you do not specify the endpoints to be discarded, the MATLAB appends zeros at both ends of the array to make them the window fit. For example, if you run
A = [4 8 6 -1 -2 -3 -1 3 4 5];
M = movsum(A,3)
then MATLAB will create matric A like this
A = [0 4 8 6 -1 -2 -3 -1 3 4 5 0];
so that window of size 3 can fit at 4 (first element) and 5 (last element)
  2 Comments
Steven Lord
Steven Lord on 24 May 2020
Continuing Ameer Hamza's example the window of length 3 centered at the first element of the vector, 4, would contain one of those "padding" 0's. If you specify that you want to 'discard' the endpoints, MATLAB will not compute the sum for windows that contain one of those padding 0's. So the first element of M is the sum for the window centered around 8, not around the first 4, as 8 is the first element whose whole window is inside the vector A.
If you'd specified that you want to 'shrink' the endpoints, MATLAB would compute the sum over the first window (centered around the first 4) and return that as the first element of M. In the case of movsum you can think of this is including a padding 0. In the case of other moving window functions, like movmean, it actually makes a bigger difference (as that first window would be the mean of two values not three.)
If you 'fill' the endpoints MATLAB pads not with 0 but with NaN so the values of the first and last windows, centered around the first 4 and the 5 respectively, are NaN.
Shivaani S
Shivaani S on 24 May 2020
Thank you for explaining it precisely Ameer Hamza and thank you Steven Lord for your detailed answer. Both helped me understand the code better.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 24 May 2020
If the window cannot overlap the ends without the window sticking out past the edge of the vector, then it essentially stops there, it doesn't move closer to the ends so any values it might compute there, say be shrinking the window or padding with zeros, are discarded. Thus, the resulting vector will be shorter by the width of window (half a window width on each end are discarded).

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!