Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

matlab.tall.movingWindow

将移动窗函数应用于数据块

说明

示例

tA = matlab.tall.movingWindow(fcn,window,tX) 当窗口在 tX 的第一个维度上移动时,对每个窗口应用一次 fcn 函数。输出 tA 是将 fcn 应用于每个窗口的结果的垂直串联。

示例

[tA,tB,...] = matlab.tall.movingWindow(fcn,window,tX,tY,...) 返回 tA,tB,... 数组,每个数组对应于 fcn 的一个输出参数,fcn 是返回多个输出的函数句柄。fcn 的输入是来自参数 tX, tY, ... 的数据的窗口。此语法具有以下要求:

  • fcn 返回的输出数目必须与从 matlab.tall.movingWindow 请求的输出数目相同。

  • fcn 的每个输出必须与第一个数据输入 tX 的类型相同。

  • 所有输出 tA,tB,... 必须具有相同的高度。

[___] = matlab.tall.movingWindow(___,Name,Value) 支持任何上述语法,且可使用一个或多个名称-值对组参数指定其他选项。例如,要调整窗口之间的步长,可以指定 'Stride' 和一个标量。或者,要在没有足够元素来完成窗口的情况下更改端点处理,您可以指定 'EndPoints' 和一个有效选项('shrink''discard' 或数值填充值)。

示例

全部折叠

使用 matlab.tall.movingWindow 计算航班到港和离港延误的移动中位数。

airlinesmall.csv 数据集创建一个数据存储,并将其转换为 tall 数组。该数据包含有关美国航班的到港和离港时间的信息。提取 ArrDelayDepDelay 变量(它们是航班延误数据的向量),以创建两者延误数据分别位于单独列中的 tall 数组。

varnames = {'ArrDelay', 'DepDelay'};
ds = tabularTextDatastore('airlinesmall.csv', 'TreatAsMissing', 'NA', ...
    'SelectedVariableNames', varnames);
tt = tall(ds);
tX = [tt.ArrDelay tt.DepDelay]
tX =

  Mx2 tall double matrix

     8    12
     8     1
    21    20
    13    12
     4    -1
    59    63
     3    -2
    11    -1
    :     :
    :     :

使用 matlab.tall.movingWindow 计算第一个维度中数据的移动中位数。使用 5000 的窗口大小。

fcn = @(x) median(x,1,'omitnan');
tA = matlab.tall.movingWindow(fcn,5000,tX)
tA =

  MxNx... tall double array

    ?    ?    ?    ...
    ?    ?    ?    ...
    ?    ?    ?    ...
    :    :    :
    :    :    :

将结果的唯一行收集到内存中。

tA = gather(unique(tA,'rows'))
Evaluating tall expression using the Local MATLAB Session:
- Pass 1 of 2: Completed in 0.74 sec
- Pass 2 of 2: Completed in 33 sec
Evaluation completed in 34 sec
tA = 31×2

   -4.0000   -2.0000
   -3.5000   -2.0000
   -3.0000   -2.0000
   -3.0000   -1.5000
   -3.0000   -1.0000
   -3.0000   -0.5000
   -3.0000         0
   -2.5000   -1.0000
   -2.5000         0
   -2.0000   -1.0000
      ⋮

使用 matlab.tall.movingWindow 将具有多个输出的函数应用于数据窗口。

从内存中的随机矩阵创建一个 tall 数组。

X = rand(1000,5);
tX = tall(X)
tX =

  1,000x5 tall double matrix

    0.8147    0.6312    0.7449    0.3796    0.4271
    0.9058    0.3551    0.8923    0.3191    0.9554
    0.1270    0.9970    0.2426    0.9861    0.7242
    0.9134    0.2242    0.1296    0.7182    0.5809
    0.6324    0.6525    0.2251    0.4132    0.5403
    0.0975    0.6050    0.3500    0.0986    0.7054
    0.2785    0.3872    0.2871    0.7346    0.0050
    0.5469    0.1422    0.9275    0.6373    0.7825
      :         :         :         :         :
      :         :         :         :         :

创建一个函数,求第一个维度中每个数据窗口的总和、均值、中位数和众数。每个输出需要在第一个维度上具有相同的大小,但在其他维度上可以具有不同大小。对于每个数据窗口,求和计算产生一个标量,而其他计算产生 1×N 向量。

将该函数保存在本地工作区中。

function [S,mn,mdn,md] = mystats(X)
  S = sum(X,[2 1]);
  mn = mean(X,1);
  mdn = median(X,1);
  md = mode(X,1);
end

注意:该函数作为局部函数包含在示例的末尾。

使用 matlab.tall.movingWindow 对窗口大小为 250 的数据应用 mystats 函数。指定四个输出参数以从 mystats 返回所有输出。使用 'EndPoints' 名称-值对组丢弃不完整的窗口。

[tS,tmn,tmdn,tmd] = matlab.tall.movingWindow(@mystats, 250, tX, 'EndPoints', 'discard')
tS =

  MxNx... tall double array

    ?    ?    ?    ...
    ?    ?    ?    ...
    ?    ?    ?    ...
    :    :    :
    :    :    :


tmn =

  MxNx... tall double array

    ?    ?    ?    ...
    ?    ?    ?    ...
    ?    ?    ?    ...
    :    :    :
    :    :    :


tmdn =

  MxNx... tall double array

    ?    ?    ?    ...
    ?    ?    ?    ...
    ?    ?    ?    ...
    :    :    :
    :    :    :


tmd =

  MxNx... tall double array

    ?    ?    ?    ...
    ?    ?    ?    ...
    ?    ?    ?    ...
    :    :    :
    :    :    :
function [S,mn,mdn,md] = mystats(X)
  S = sum(X,[2 1]);
  mn = mean(X,1);
  mdn = median(X,1);
  md = mode(X,1);
end

输入参数

全部折叠

要应用的窗口函数,指定为函数句柄或匿名函数。fcn 的每个输出必须与第一个输入 tX 的类型相同。您可以使用 'OutputsLike' 选项返回不同数据类型的输出。

fcn 的一般函数签名是

[a, b, c, ...] = fcn(x, y, z, ...)
fcn 必须满足以下要求:

  1. 输入参数 - 输入 [x, y, z, ...] 是内存数据块。通过分别从各个 tall 数组输入 [tX, tY, tZ, ...] 中提取数据来生成这些数据块。输入 [x, y, z, ...] 满足以下属性:

    • 所有输入 [x, y, z, ...] 在第一个维度中具有相同的大小。

    • [x, y, z, ...] 中的数据块来自 tall 维度中的相同索引(假设 tall 数组的 tall 维度是非单一维度)。例如,如果 tXtY 的 tall 维度为非单一维度,则第一组数据块可能是 x = tX(1:20000,:)y = tY(1:20000,:)

    • 当任一 [tX, tY, tZ, ...] 的第一个维度的大小为 1 时,对应的块 [x, y, z, ...] 包含该 tall 数组中的所有数据。

    • 应用 fcn 对输入数据进行约简后,结果必须是标量或高度为 1 的数组切片。

      当输入是矩阵、N 维数组、表或时间表时,应用 fcn 必须对输入数据的每列或每个变量都进行约简。

  2. 输出参数 - 输出 [a, b, c, ...] 是可放入内存的数据块,将被发送到对应的输出 [tA, tB, tC, ...]。输出 [a, b, c, ...] 满足以下属性:

    • 所有输出 [a, b, c, ...] 的第一个维度必须具有相同的维度。

    • 所有输出 [a, b, c, ...] 与先前对 fcn 的调用的对应结果垂直串联。

    • 所有输出 [a, b, c, ...] 都将发送到各自目标输出数组中第一个维度中的相同索引。

  3. 函数规则 - fcn 必须满足函数规则:

    • F([inputs1; inputs2]) == [F(inputs1); F(inputs2)]:将函数应用于输入的串联应该等效于将函数分别应用于各输入,然后对结果进行串联。

例如,此函数计算窗口中元素的均值和标准差,并返回两个输出数组:

function [mv,sd] = movstats(tX)
mv = mean(tX,1,'omitnan');
sd = std(tX,1,'omitnan');
end
将此函数保存到可访问的文件夹后,可以使用以下命令调用窗口大小为 5 的函数:
[tA,tB] = matlab.tall.movingWindow(@movstats,5,tX)

示例: tA = matlab.tall.movingWindow(@(x) std(x,1,'omitnan'), tX) 指定匿名函数来计算每个窗口的标准差,忽略 NaN

示例: tA = matlab.tall.movingWindow(@mean,3,tX) 指定函数句柄 @mean 来计算每个三元素窗口的均值。

数据类型: function_handle

窗口大小,指定为正整数标量或二元素行向量 [NB NF]

  • 如果 window 是标量,则:

    • 当窗口大小为奇数时,每个窗口都以数据中对应的元素为中心。

      Illustration of a window size of three for a vector with six elements. There are six windows and the first and last windows have two elements such that each window is centered on the corresponding element in the data.

    • 当窗口大小为偶数时,每个窗口都以当前元素及其前一个元素为中心。

      Illustration of a window size of four for a vector with six elements. The first window has two elements, the second has three elements, the next three windows have four elements, and the last window has three elements. Each window is centered about the current and previous elements.

  • 如果 window 是向量 [NB NF],则窗口包括输入的当前元素及其前面 NB 个元素和后面 NF 个元素。

    Illustration of a window size of [2 2] for a vector with six elements. The first window has three elements, the second has four elements, the next two windows have five elements, the second-to-last window has four elements, and the last window has three elements. Each window includes two previous values (when possible), the current value, and the next two values (when possible).

默认情况下,在端点处如果没有足够元素充满整个窗口大小时,窗口会自动在端点处截断。当窗口以这种方式截断时,函数仅应用于窗口内所含的元素。您可以使用 EndPoints 名称-值对组来更改此行为。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

输入数组,指定为标量、向量、矩阵、多维数组、表或时间表的单独参数。输入数组可以是 tall 数组或内存数组。输入数组用作变换函数 fcn 的输入。每个输入数组 tX,tY,... 必须具有相同的高度。

名称-值参数

将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参数名称,Value 是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: tA = matlab.tall.movingWindow(@myFcn, window, tX, 'Stride', 2)

窗口之间的步长,指定为包含 'Stride' 和正整数标量的逗号分隔对组。在 fcn 对数据窗口进行操作后,在对下一个窗口进行操作之前,计算将按照 'Stride' 值步进。将 'Stride' 的值从默认值 1 增加等于通过每隔一个元素或每隔两个元素等进行选取来减小输出的大小。

默认情况下,'Stride' 的值是 1,因此每个窗口都以输入中的每个元素为中心。例如,此处有一个移动和计算,窗口大小为 3,对向量 [1 2 3 4 5 6]' 进行操作:

Illustration of a moving sum on a vector with six elements utilizing a stride value of 1. A total of six windows are used in the calculation, so the output has six elements.

如果 'Stride' 的值是 2,则计算会发生变化,每个窗口以输入中的每第二个元素为中心 (1, 3, 5)。移动和现在返回三个部分和,而不是六个:

Illustration of a moving sum on a vector with six elements utilizing a stride value of 2. A total of three windows are used in the calculation, so the output has three elements.

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

用于处理前导和尾部窗口的方法,指定为以逗号分隔的对组,包含 'EndPoints' 和下表中的值之一。

在加窗计算的开始和结束时,正在操作的元素的窗口是不完整的。'EndPoints' 选项指定如何处理这些不完整的窗口。

'EndPoints'描述示例:移动总和

'shrink'

在窗接近输入端点时缩小窗口的大小,从而只包括现有元素。

Illustration of a moving sum on a vector with six elements. Six windows are used in the moving sum, with the windows at the endpoints including two elements and interior windows including three elements.

'discard'

当窗口不与现有元素完全重叠时,不输出任何结果。

Illustration of a moving sum on a vector with six elements. Four windows are used in the moving sum, with all windows including three elements.

数值或逻辑填充值

将不存在的元素替换为指定的数值或逻辑值。

  • 填充值的类型必须与 tX 相同。

  • 第一个维度中填充值的大小必须等于 1,其他维度中的大小必须匹配 tX

Illustration of a moving sum on a vector with six elements. Six windows are used in the moving sum, with the windows at the endpoints including two elements plus a fill value. The interior windows have three elements.

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string

输出数组的原型,以逗号分隔的对组形式指定,由 'OutputsLike' 和包含原型数组的元胞数组组成。当您指定 'OutputsLike' 时,matlab.tall.movingWindow 返回的输出数组 tA,tB,... 与指定的原型数组 {PA,PB,...} 具有相同的数据类型和属性。每当输出数组的数据类型不同于输入数组的数据类型时,您必须指定 'OutputsLike'。如果指定 'OutputsLike',则必须为每个输出指定一个原型数组。

示例: tA = matlab.tall.movingWindow(..., tX, 'OutputsLike', {int8(1)});(其中 tX 是双精度 tall 数组)以 int8(而不是 double)形式返回 tA

数据类型: cell

输出参数

全部折叠

输出数组,以标量、向量、矩阵或多维数组形式返回。如果 matlab.tall.movingWindow 的任一输入为 tall,则所有输出参数也为 tall。否则,所有输出参数均为内存数组。

  • 输出数组的大小和数据类型取决于指定的窗口函数 fcn

  • 输出数组 tA,tB,... 都具有相同的高度,这取决于 'Stride''EndPoints' 的值。默认情况下,输出数组与输入数组大小相同。

  • 通常,输出 tA,tB,... 必须与第一个输入 tX 具有相同的数据类型。但是,您可以指定 'OutputsLike' 以返回不同数据类型。如果输入数组 tX, tY, ... 为空,或 'EndPoints''discard' 且没有足够的元素来填充完整大小的窗口,matlab.tall.movingWindow 将返回空输出。空输出的大小基于输入数组 tX 的大小,或基于提供给 'OutputsLike' 的原型数组的大小(如果指定)。

提示

  • matlab.tall.movingWindow 用于简单的滑动窗计算。matlab.tall.blockMovingWindow 是一个高级 API,可更灵活地对 tall 数组执行滑动窗计算。因此,它使用起来更加复杂,原因在于函数必须精确地处理包含许多完整窗口的数据块。但是,通过适当的向量化计算,您可以减少必要的函数调用数量并提高性能。

版本历史记录

在 R2019a 中推出