Matlab数据处理之移动均值函数movmean

4.8
(4)

Matlab中,可以使用movmean函数表示一组数据的移动均值。移动均值,由局部 k 个数据点的均值组成的数组。关于均值函数mean的文章,可以参考:《Matlab数据处理之mean函数获取数组平均值》。

Matlab数据处理之移动均值函数movmean

本文主要讲解Matlab中movmean函数的常见用法、语法说明,以及一些相关实例。首先,给出movmean函数的帮助文档如下:

>> help movmean
 movmean   Moving mean value.
    Y = movmean(X,K) for a vector X and positive integer scalar K computes
    a centered moving average by sliding a window of length K along X. Each
    element of Y is the local mean of the corresponding values of X inside
    the window, with Y the same size as X. When K is even, the window is
    centered about the current and previous elements of X. The sliding
    window is truncated at the endpoints where there are fewer than K
    elements from X to fill the window.
    
    For N-D arrays, movmean operates along the first array dimension whose
    size does not equal 1.
 
    Y = movmean(X,[NB NF]) for a vector X and nonnegative integers NB and
    NF computes a moving average along the length of X, returning the local
    mean of the previous NB elements, the current element, and the next NF
    elements of X.
 
    Y = movmean(...,DIM) operates along dimension DIM of X.
 
    movmean(...,MISSING) specifies how NaN (Not-a-Number) values are
    treated and can be one of the following:
 
        'includenan'   - (default) the mean of any window containing NaN
                         values is also NaN.
        'omitnan'      - the mean of any window containing NaN values is
                         the mean of all its non-NaN elements. If all
                         elements are NaN, the result is NaN.
 
    movmean(...,'Endpoints',ENDPT) controls how the mean is calculated at
    the endpoints of X, where there are not enough elements to fill the
    window. ENDPT can be either a scalar numeric or logical value or one of
    the following:
 
        'shrink'    - (default) compute the mean over the number of
                      elements of X that are inside the window, effectively
                      reducing the window size to fit X at the endpoints.
        'fill'      - compute the mean over the full window size, filling
                      missing values from X with NaN. This is equivalent to
                      padding X with NaN at the endpoints.
        'discard'   - compute the mean only when the window is filled with
                      elements of X, discarding partial endpoint
                      calculations and their corresponding elements in Y.
                      This truncates the output; for a vector X and window
                      length K, Y has length LENGTH(X)-K+1.
                      
    When ENDPT is a scalar numeric or logical value, the missing elements
    of X inside the window are replaced with that value and Y remains the
    same size as X.
 
    Example: Compute a 5-point centered moving average.
        t = 1:10;
        x = [4 8 6 -1 -2 -3 -1 3 4 5];
        yc = movmean(x,5);
        plot(t,x,t,yc);
 
    Example: Compute a 5-point trailing moving average.
        t = 1:10;
        x = [4 8 6 -1 -2 -3 -1 3 4 5];
        yt = movmean(x,[4 0]);
        plot(t,x,t,yt);
 
    Example: Compute a 5-point centered moving average, padding the ends of
    the input with NaN.
        t = 1:10;
        x = [4 8 6 -1 -2 -3 -1 3 4 5];
        yp = movmean(x,5,'Endpoints','fill');
        plot(t,x,t,yp);
 
    Example: Compute a 5-point trailing moving average, ignoring the first
    4 window shifts that do not contain 5 input elements.
        x = [4 8 6 -1 -2 -3 -1 3 4 5];
        yd = movmean(x,[4 0],'Endpoints','discard');

movmean函数常见用法

M = movmean(A,k)
M = movmean(A,[kb kf])
M = movmean(___,dim)
M = movmean(___,nanflag)
M = movmean(___,Name,Value)

movmean函数语法说明

M = movmean(A,k) 返回由局部 k 个数据点的均值组成的数组,其中每个均值是基于 A 的相邻元素的长度为 k 的滑动窗计算得出。当 k 为奇数时,窗口以当前位置的元素为中心。当 k 为偶数时,窗口以当前元素及其前一个元素为中心。当没有足够的元素填满窗口时,窗口将自动在端点处截断。当窗口被截断时,只根据窗口内的元素计算平均值。M 与 A 的大小相同。

  • 如果 A 是向量,movmean 将沿向量 A 的长度运算。
  • 如果 A 是多维数组,则 movmean 沿 A 的大小不等于 1 的第一个维度进行运算。

M = movmean(A,[kb kf]) 通过长度为 kb+kf+1 的窗口计算均值,其中包括当前位置的元素、前面的 kb 个元素和后面的 kf 个元素。

M = movmean(_,dim) 为上述任一语法指定 A 的运算维度。例如,如果 A 是矩阵,则 movmean(A,k,2) 沿 A 的列运算,计算每行的 k 元素移动均值。

M = movmean(_,nanflag) 指定在上述任意语法的计算中包括还是忽略 NaN 值。movmean(A,k,’includenan’) 会在计算中包括所有 NaN 值,而 movmean(A,k,’omitnan’) 则忽略这些值并基于较少的点计算均值。

M = movmean(_,Name,Value) 使用一个或多个名称-值对组参数指定移动平均值的其他参数。例如,如果 x 是时间值向量,则 movmean(A,k,’SamplePoints’,x) 相对于 x 中的时间计算移动平均值。

movmean函数实例

向量的中心移动平均值

计算行向量的三点中心移动平均值。当端点处的窗口中少于三个元素时,将根据可用元素计算平均值。

>> A = [4 8 6 -1 -2 -3 -1 3 4 5];
>> M = movmean(A,3)

M =

    6.0000    6.0000    4.3333    1.0000   -2.0000   -2.0000   -0.3333    2.0000    4.0000    4.5000

向量的尾部移动平均值

计算行向量的三点尾部移动平均值。当端点处的窗口中少于三个元素时,将根据可用元素计算平均值。

>> A = [4 8 6 -1 -2 -3 -1 3 4 5];
>> M = movmean(A,[2 0])

M =

    4.0000    6.0000    6.0000    4.3333    1.0000   -2.0000   -2.0000   -0.3333    2.0000    4.0000

矩阵的移动平均值

计算矩阵中每行的三点中心移动平均值。窗口从第一行开始,沿水平方向移动到该行的末尾,然后移到第二行,依此类推。维度参数为 2,即跨 A 的列移动窗口。

>> A = [4 8 6; -1 -2 -3; -1 3 4]

A =

     4     8     6
    -1    -2    -3
    -1     3     4

>> M = movmean(A,3,2)

M =

    6.0000    6.0000    7.0000
   -1.5000   -2.0000   -2.5000
    1.0000    2.0000    3.5000
Matlab数据处理之移动均值函数movmean

包含 NaN 元素的向量的移动平均值

计算包含两个 NaN 元素的行向量的三点中心移动平均值。

>> A = [4 8 NaN -1 -2 -3 NaN 3 4 5];
>> M = movmean(A,3)

M =

    6.0000       NaN       NaN       NaN   -2.0000       NaN       NaN       NaN    4.0000    4.5000

重新计算平均值,但忽略 NaN 值。当 movmean 舍弃 NaN 元素时,它将根据窗口中的剩余元素计算平均值。

>> M = movmean(A,3,'omitnan')

M =

    6.0000    6.0000    3.5000   -1.5000   -2.0000   -2.5000         0    3.5000    4.0000    4.5000
Matlab数据处理之移动均值函数movmean

基于样本点计算移动平均值

根据时间向量 t,计算 A 中数据的 3 小时中心移动平均值。

>> A = [4 8 6 -1 -2 -3];
>> k = hours(3);
>> t = datetime(2016,1,1,0,0,0) + hours(0:5)

t = 

1 至 5 列

   2016-01-01 00:00:00   2016-01-01 01:00:00   2016-01-01 02:00:00   2016-01-01 03:00:00   2016-01-01 04:00:00

6 列

   2016-01-01 05:00:00

由于我的版本过低,计算平均值会报错:

>> M = movmean(A,k,'SamplePoints',t)
错误使用 movmean
窗口长度必须为有限标量正整数,或者为包含有限非负整数的 2 元矢量。

下面给出官方的答案

M = 1×6

    6.0000    6.0000    4.3333    1.0000   -2.0000   -2.5000
Matlab数据处理之移动均值函数movmean

共计4人评分,平均4.8

到目前为止还没有投票~

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

文章目录

原创文章,作者:古哥钻石会员,转载需经过作者授权同意,并附上原文链接:https://iymark.com/articles/4186.html

(0)
微信公众号
古哥的头像古哥钻石会员管理团队
上一篇 2023年03月05日 21:38
下一篇 2023年03月06日 21:20

你可能感兴趣的文章

发表回复

登录后才能评论
微信小程序
微信公众号