Main Content

movmean

Moving mean

Description

example

M = movmean(A,k)返回一个数组的地方k-pointmeanvalues, where each mean is calculated over a sliding window of lengthkacross neighboring elements ofA. Whenkis odd, the window is centered about the element in the current position. Whenkis even, the window is centered about the current and previous elements. The window size is automatically truncated at the endpoints when there are not enough elements to fill the window. When the window is truncated, the average is taken over only the elements that fill the window.M都是一样的大小asA.

  • IfAis a vector, thenmovmeanoperates along the length of the vector.

  • IfAis a multidimensional array, thenmovmeanoperates along the first array dimension whose size does not equal 1.

example

M = movmean(A,[kb kf])computes the mean with a window of lengthkb+kf+1that includes the element in the current position,kbelements backward, andkfelements forward.

example

M = movmean(___,dim)returns the array of moving averages along dimensiondimfor any of the previous syntaxes. For example, ifAis a matrix, thenmovmean(A,k,2)operates along the columns ofA, computing thek-element sliding mean for each row.

example

M = movmean(___,nanflag)specifies whether to include or omitNaNvalues from the calculation for any of the previous syntaxes.movmean(A,k,'includenan')includes allNaNvalues in the calculation whilemovmean(A,k,'omitnan')ignores them and computes the mean over fewer points.

example

M = movmean(___,Name,Value)specifies additional parameters for the moving average using one or more name-value pair arguments. For example, ifxis a vector of time values, thenmovmean(A,k,'SamplePoints',x)computes the moving average relative to the times inx.

Examples

collapse all

Compute the three-point centered moving average of a row vector. When there are fewer than three elements in the window at the endpoints, take the average over the elements that are available.

A = [4 8 6 -1 -2 -3 -1 3 4 5]; M = movmean(A,3)
M =1×106.0000 6.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000 4.5000

Compute the three-point trailing moving average of a row vector. When there are fewer than three elements in the window at the endpoints, take the average over the elements that are available.

A = [4 8 6 -1 -2 -3 -1 3 4 5]; M = movmean(A,[2 0])
M =1×104.0000 6.0000 6.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000

Compute the three-point centered moving average for each row of a matrix. The window starts on the first row, slides horizontally to the end of the row, then moves to the second row, and so on. The dimension argument is two, which slides the window across the columns ofA.

A = [4 8 6; -1 -2 -3; -1 3 4]
A =3×34 8 6 -1 -2 -3 -1 3 4
M = movmean(A,3,2)
M =3×36.0000 6.0000 7.0000 -1.5000 -2.0000 -2.5000 1.0000 2.0000 3.5000

Compute the three-point centered moving average of a row vector containing twoNaNelements.

A = [4 8 NaN -1 -2 -3 NaN 3 4 5]; M = movmean(A,3)
M =1×106.0000 NaN NaN NaN -2.0000 NaN NaN NaN 4.0000 4.5000

Recalculate the average, but omit theNaNvalues. WhenmovmeandiscardsNaNelements, it takes the average over the remaining elements in the window.

M = movmean(A,3,'omitnan')
M =1×106.0000 6.0000 3.5000 -1.5000 -2.0000 -2.5000 0 3.5000 4.0000 4.5000

Compute a 3-hour centered moving average of the data inAaccording to the time vectort.

A = [4 8 6 -1 -2 -3]; k = hours(3); t = datetime(2016,1,1,0,0,0) + hours(0:5)
t =1x6 datetimeColumns 1 through 3 01-Jan-2016 00:00:00 01-Jan-2016 01:00:00 01-Jan-2016 02:00:00 Columns 4 through 6 01-Jan-2016 03:00:00 01-Jan-2016 04:00:00 01-Jan-2016 05:00:00
M = movmean(A,k,'SamplePoints',t)
M =1×66.0000 6.0000 4.3333 1.0000 -2.0000 -2.5000

Compute the three-point centered moving average of a row vector, but discard any calculation that uses fewer than three points from the output. In other words, return only the averages computed from a full three-element window, discarding endpoint calculations.

A = [4 8 6 -1 -2 -3 -1 3 4 5]; M = movmean(A,3,'Endpoints','discard')
M =1×86.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Window length, specified as a numeric or duration scalar. Whenkis a positive integer scalar, the centered average includes the element in the current position plus surrounding neighbors. For example, a three-point average defined by a window of length three results in the following calculation for a vectorA:

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|duration

Directional window length, specified as a numeric or duration row vector containing two elements. Whenkbandkfare positive integer scalars, the calculation is overkb+kf+1elements. The calculation includes the element in the current position,kbelements before the current position, andkfelements after the current position. For example, a four-point average defined by the directional window[2 1]results in the following calculation for a vectorA:

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|duration

Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.

Dimensiondimindicates the dimension thatmovmeanoperates along, that is, the direction in which the specified window slides.

Consider a two-dimensional input array,A.

  • Ifdim = 1, thenmovmean(A,k,1)starts with the first column and slides vertically over each row. The mean is computed overkelements at a time. Then it moves to the second column and repeats the computation. This process continues until all columns are exhausted.

  • Ifdim = 2, thenmovmean(A,k,2)starts with the first row and slides horizontally across each column. The mean is computed overkelements at a time. Then it moves to the second row and repeats the computation. This process continues until all rows are exhausted.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

NaNcondition, specified as one of these values:

  • “includenan”— IncludeNaNvalues from the input when computing the mean, resulting inNaNoutput.

  • 'omitnan'— Ignore allNaNvalues in the input. If a window contains onlyNaNvalues, thenmovmeanreturnsNaN.

Name-Value Pair Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:M = movmean(A,k,'Endpoints','fill')

Method to treat leading and trailing windows, specified as the comma-separated pair consisting of'Endpoints'and one of the following:

'Endpoints'Value Description
'shrink' Shrink the window size near the endpoints of the input to include only existing elements.
'discard' Do not output any averages when the window does not completely overlap with existing elements.
'fill' Substitute nonexisting elements withNaN.
numeric or logical scalar Substitute nonexisting elements with a specified numeric or logical value.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|string

Sample points for computing averages, specified as the comma-separated pair consisting of'SamplePoints'和一个向量。中心的采样点表示tion of the data inA. Sample points do not need to be uniformly sampled. By default, the sample points vector is[1 2 3 ... ].

Moving windows are defined relative to the sample points, which must be sorted and contain unique elements. For example, iftis a vector times corresponding to the input data, thenmovmean(rand(1,10),3,'SamplePoints',t)has a window that represents the time interval betweent(i)-1.5andt(i)+1.5.

When the sample points vector has data typedatetimeorduration, then the moving window length must have typeduration.

If the sample points are nonuniformly spaced and the'Endpoints'name-value pair is specified, then its value must be'shrink'.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|datetime|duration

More About

collapse all

Mean

For a random variable vectorAmade up ofNscalar observations, the mean is defined as

μ = 1 N i = 1 N A i .

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2016a