movsum

Moving sum over a sliding window.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

M = movsum(A, wlen)

Sums over a window of wlen elements centered on each element.

M = movsum(A, [nb na])

Uses a window of nb elements before and na after the current one.

M = movsum(___, nanflag)

Use 'omitnan' to ignore NaN values, or 'includenan' (default).

M = movsum(___, 'Endpoints', ep)

Chooses what to do at the edges, for example 'shrink' (default), 'discard' or 'fill'.

Description

With the default 'shrink', windows that stick out past the ends are cut to the available elements, so the first and last values sum fewer terms. movsum(1:6, 3) is 3 6 9 12 15 11: the first window is [1 2] and the last is [5 6].

A moving sum is the basis of moving averages (movmean is the same sum divided by the window size) and of simple smoothing.

Input arguments

A
Numeric vector or matrix; a matrix is processed column by column.
wlen
Window length. An odd value is centered; an even value includes one more element before the current one than after.
[nb na]
Number of elements before and after the current one.
'Endpoints'
Edge handling: 'shrink', 'discard', 'fill' or a number.

Output arguments

M
Array of window sums. Same size as A unless 'discard' is used.

Example

movsum(1:6, 3)
movsum(1:6, [1 0])
movsum(1:6, 3, 'Endpoints', 'discard')
movsum([1 NaN 3 4], 3, 'omitnan')
Output
ans = 1×6

     3     6     9    12    15    11
ans = 1×6

     1     3     5     7     9    11
ans = 1×4

     6     9    12    15
ans = 1×4

     1     4     7     7

See also

movmean cumsum sum conv smoothdata