Matlab数据处理之移动最大值函数movmax

文章目录

展开
4.7
(3)

Matlab中,可以使用movmax函数表示一组数据的移动最大值。移动最大值,由局部 k 个数据点范围内的最大值组成的数组。关于最大值函数max,可以参考:《Matlab数据处理之max最值函数的用法介绍》。

Matlab数据处理之移动最大值函数movmax

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

>> help movmax
 movmax   Moving maximum value.
    Y = movmax(X,K) for a vector X and positive integer scalar K computes a
    centered moving maximum by sliding a window of length K along X. Each
    element of Y is the local maximum 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, movmax operates along the first array dimension whose
    size does not equal 1.
 
    Y = movmax(X,[NB NF]) for a vector X and nonnegative integers NB and NF
    computes a moving maximum along the length of X, returning the local
    maximum of the previous NB elements, the current element, and the next
    NF elements of X.
 
    Y = movmax(...,DIM) operates along dimension DIM of X.
 
    movmax(...,MISSING) specifies how NaN (Not-a-Number) values are treated
    and can be one of the following:
 
        'omitnan'      - (default) the maximum of any window containing NaN
                         values is the maximum of all its non-NaN elements.
                         If all elements are NaN, the result is NaN.
        'includenan'   - the maximum of any window containing NaN values is
                         also NaN.
 
    movmax(...,'Endpoints',ENDPT) controls how the maximum 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 maximum 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 maximum over the full window size,
                      filling missing values from X with -Inf. This is
                      equivalent to padding X with -Inf at the endpoints.
        'discard'   - compute the maximum 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 maximum.
        t = 1:10;
        x = [4 3 1 -2 -4 -1 0 2 -2 3];
        yc = movmax(x,5);
        plot(t,x,t,yc);
 
    Example: Compute a 5-point trailing moving maximum.
        t = 1:10;
        x = [4 3 1 -2 -4 -1 0 2 -2 3];
        yt = movmax(x,[4 0]);
        plot(t,x,t,yt);
 
    Example: Compute a 5-point centered moving maximum, padding the ends of
    the input with -Inf.
        t = 1:10;
        x = [4 3 1 -2 -4 -1 0 2 -2 3];
        yp = movmax(x,5,'Endpoints','fill');
        plot(t,x,t,yp);
 
    Example: Compute a 5-point trailing moving maximum, ignoring the first
    4 window shifts that do not contain 5 input elements.
        x = [4 3 1 -2 -4 -1 0 2 -2 3];
        yd = movmax(x,[4 0],'Endpoints','discard');

movmax函数常见用法

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

movmax函数语法说明

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

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

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

M = movmax(_,dim) 为上述任意语法返回维度 dim 上的移动最大值数组。例如,如果 A 是矩阵,则 movmax(A,k,2) 沿 A 的列运算,计算每一行的 k 个元素的移动最大值。

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

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

movmax函数实例

向量的中心移动最大值

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

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

M =

     8     8     8     6    -1    -1     3     4     5     5

向量的尾部移动最大值

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

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

M =

     4     8     8     8     6    -1    -1     3     4     5

矩阵的移动最大值

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

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

A =

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

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

M =

     8     8     8
    -1    -1    -2
     3     4     4
Matlab数据处理之移动最大值函数movmax

包含 NaN 元素的向量的移动最大值

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

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

M =

     8     8     8    -1    -1    -2     3     4     5     5

重新计算最大值,但包括 NaN 值。当计算包含至少一个 NaN 值的一组元素的最大值时,movmax 将返回 NaN

>> M = movmax(A,3,'includenan')

M =

     8   NaN   NaN   NaN    -1   NaN   NaN   NaN     5     5

基于样本点计算移动最大值

根据时间向量 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 = movmax(A,k,'SamplePoints',t)
错误使用 movmax
窗口长度必须为有限标量正整数,或者为包含有限非负整数的 2 元矢量。

版本问题报错了,我用的2016版本,以下为官网给的结果

>> M = movmax(A,k,'SamplePoints',t)

M = 1×6

     8     8     8     6    -1    -2
Matlab数据处理之移动最大值函数movmax

仅返回满窗口最大值

计算行向量的三点中心移动最大值,但在输出中舍弃使用的点数少于三个的计算。也就是说,只返回从满的三元素窗口计算的最大值,而舍弃端点计算。

>> A = [4 8 6 -1 -2 -3 -1 3 4 5];
>> M = movmax(A,3,'Endpoints','discard')

M =

     8     8     6    -1    -1     3     4     5

共计3人评分,平均4.7

到目前为止还没有投票~

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

让我们改善这篇文章!

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

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

(0)
上一篇 2023年03月18日 20:36
下一篇 2023年03月20日 00:10

你可能感兴趣的文章

  • Matlab极坐标绘制直方图polarhistogram

    文章目录 展开常见用法语法说明在极坐标中创建直方图为极坐标直方图指定 bin 数修改直方图外观创建直方图之后修改其外观 3 (4) 今天,来说下Matlab中用极坐标体系绘制直方图…

    2020年12月26日
    0
  • Matlab三维散点图绘制函数scatter3

    文章目录 展开常见用法语法说明创建三维散点图改变标记大小改变标记颜色填充标记设置标记类型设置标记属性指定三维散点图的坐标区使用句柄设置散点序列属性 3.3 (4) 今天,带来Mat…

    2020年11月10日
    0
  • Matlab绘制极坐标函数图像ezpolar

    文章目录 展开常见用法语法说明数字函数的极坐标图创建 4.3 (4) 今天,带来关于Matlab极坐标中的最后一篇系列文章,本文讲解绘制极坐标函数图像的方法,所用到的函数为ezpo…

    2021年01月06日
    0
  • Matlab三维饼图绘制函数pie3用法

    文章目录 展开常见用法语法说明创建三维饼图指定三维饼图的文本标签比较两个饼图 4.3 (4) 今天,带来Matlab中绘制三维饼图的函数pie3的使用方法,饼图将将数据分割,然后每…

    2020年11月07日
    0
  • Matlab矩阵散点图绘制函数plotmatrix

    文章目录 展开常见用法语法说明使用两个矩阵输入创建散点图矩阵使用一个矩阵输入创建散点图矩阵指定标记类型和颜色创建并修改散点图矩阵 4.4 (5) 今天,给各位带来Matlab中,使…

    2020年11月15日
    0
  • Matlab数据处理之累积乘积函数cumprod

    文章目录 展开cumprod函数常见用法cumprod函数语法说明cumprod函数实例向量的累计乘积矩阵中每列的累计乘积矩阵中每行的累计乘积逻辑值输入双精度输出反向累积乘积包含 …

    2023年03月25日
    0

发表回复

登录后才能评论
本站APP
微信小程序