带有滑动窗口元素的矩阵 [英] Matrix with sliding window elements

查看:131
本文介绍了带有滑动窗口元素的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时间序列,并且将一些用户定义的函数应用于时间序列中的每个W元素.

I have time series, and I applying some user defined function to every W elements in time series.

现在,我仅使用for循环,将大小为W的滑动窗口在每次迭代中应用于窗口中的元素.

Right now I am just using for loop, slide window of size W an apply my function to elements in a window at every iteration.

我正在使用Matlab,使用"for循环"的效率很低,所以我想对这一操作进行矢量化处理.

I am using Matlab and it is very inefficient with a "for loops" so I would love to vectorize this operation.

作为一种解决方案,我看到将长度为N的信号转换为大小为(N-1,W)的矩阵,其中每一行都是在不同窗口中的时间序列,并将函数应用于该矩阵.

As a solution I see transforming signal with length N to a matrix with size (N - 1, W) where each row is time series in different windows and applying function to this matrix.

所以,我的问题是:

  1. 如何将我的初始时间序列转换为这样的矩阵?
  2. 假设我正在用步骤X滑动窗口.因此,不会出现((N-1,W)矩阵,而是((N-1)/X,W). ([1]中矩阵的第X行)

示例:

比方说我的时间序列是:

Let's say my time series is:

T = [1, 5, 6, 8, 10, 14, 22]
W = 3
X = 1

=>我很想得到

[[1, 5, 6], 
[5, 6, 8], 
[6, 8, 10],
[8, 10, 14],
[10, 14, 22]]

如果

W = 3
X = 2

=>我很想得到

[[1, 5, 6], 
[6, 8, 10],
[10, 14, 22]]

推荐答案

使用

Creating the right indices with bsxfun should most certainly help:

ind = bsxfun(@plus, 1:W, (0:X:numel(T)-W).');
out = T(ind);  

创建正确的索引是第一步,由第一行代码描述.该代码的作用是创建一个2D矩阵,其中每一行都是每个感兴趣窗口要访问的元素.如果您想了解代码如何生成索引的直观信息,请专门研究第一种情况,其中X = 1;W = 3;.

Creating the right indices is the first step, delineated by the first line of code. What this code does is that it creates a 2D matrix where each row are the elements to access per window of interest. If you want to gain intuition on how the code generates the indices, look specifically at the first case where X = 1; and W = 3;.

我们可以看到第一行包含访问元素1、2、3.第二行包含访问元素2、3、4 ...直到最后一行,即5、6、7.可以看到我们必须访问窗口中的相邻元素,因此基本索引需要从1、2、3或通常从1到W.现在,我们需要偏移,以使它们位于每个窗口T中正确的元素的中心.第一个窗口的偏移量仅是0,第二个窗口的下一个偏移量仅是1,直到最后一行为3.我们看到,对于每一行,随着行数的增加,我们在基本索引上又增加了1.因此,我们为第二行的每个基本索引添加1,然后为第三行的每个基本索引添加2,依此类推.如果将基本索引与偏移索引相加,则最终将获得正确的索引以访问T中的正确元素.

We can see that the first row consists of accessing elements 1, 2, 3. The second row consists of accessing elements 2, 3, 4... up until the last row, which is 5, 6, 7. We can see that we have to access neighbouring elements in a window, and so the base indices need to go from 1, 2, 3, or in general from 1 to W. We now need to offset these indices so that they are centred at the right elements in T per window. The offset for the first window is simply 0, the next offset for the second window is simply 1 up until the last row which is 3. We see that for each row, we add 1 more to the base indices as the rows increase. Therefore, we add 1 to each base index for the second row, then 2 for each base index in the third row and so on. If you add the base indices with the offset indices, you thus finally get the correct indices to access the right elements in T.

类似地,如果X = 2;W = 3;,我们看到我们的基本索引仍然是1、2、3.但是,现在访问的正确元素是第一行的1、2、3,然后是3, 4、5用于第二行,然后5、6、7用于第三行.对于每一行,我们现在将基本索引偏移 2 而不是现在的1.因此,第二行向每个基本索引添加2,然后为第三行向每个基本索引添加4,依此类推.

Similarly if X = 2; and W = 3;, we see that we still have base indices of 1, 2, 3. However, the right elements to access now are 1, 2, 3 for the first row, then 3, 4, 5 for the second row then 5, 6, 7 for the third row. For each row, we now offset the base indices by 2 instead of 1 now. Therefore the second row we add 2 to each base index, then we add 4 to each base index for the third row and so on.

通常,基本索引是使用向量1:W创建的,而偏移索引是使用向量0:X:numel(T)-W创建的. W的减法是必需的,这样根据要求对信号进行采样时,我们就不会超出范围.为了创建我们刚才讨论的这些索引,bsxfun为我们处理了这个.

In general, the base indices are created using a vector 1:W and the offset indices are created using a vector 0:X:numel(T)-W. The subtraction of W is required so that we don't go out of bounds when sampling the signal as per the requirement. To create these indices that we just talked about, bsxfun handles this for us.

我们创建与基本索引相对应的行向量1:W和与每个窗口的偏移量相对应的列向量(0:X:numel(T)-W).'.请注意,第一个偏移量从0开始,然后我们增加X的量,以确保计算出正确的中心以将基本索引放置在该位置.我们停下来,直到点击numel(T)-W元素,这就是您所陈述的条件.通过使用bsxfun,创建两个临时2D矩阵,其中行向量在列向量中复制的行数与列向量中的行数相同,列向量在列向量中复制的列数与行向量中的行数相同.将这两个矩阵加在一起后,便得到了结果索引矩阵.

We create a row vector of 1:W which corresponds to the base indices and a column vector of (0:X:numel(T)-W).' which corresponds to the offsets per window. Note that the first offset starts at 0, then we increment by X amount to ensure that the correct centre is calculated to place our base indices at. We stop until we hit numel(T)-W elements, which is the condition you have stated. By using bsxfun, two temporary 2D matrices are created where the row vector is duplicated for as many rows as there are rows as there are in the column vector and the column vector is duplicated for as many columns as there are in the row vector. Once you add these two matrices together, you get the resulting index matrix.

使用W = 3;X = 1;运行代码会给出:

Running the code with W = 3; and X = 1; gives:

>> T = [1, 5, 6, 8, 10, 14, 22];
>> X = 1;
>> W = 3;
>> ind = bsxfun(@plus, 1:W, (0:X:numel(T)-W).')

ind =

     1     2     3
     2     3     4
     3     4     5
     4     5     6
     5     6     7

类似地,如果W = 3;X = 2;我们也会得到:

Similarly if W = 3; and X = 2; we also get:

>> T = [1, 5, 6, 8, 10, 14, 22];
>> X = 2;
>> W = 3;
>> ind = bsxfun(@plus, 1:W, (0:X:numel(T)-W).')

ind =

     1     2     3
     3     4     5
     5     6     7

您可以亲自验证这些索引是否与T中的正确元素相对应,以在这种情况下创建所需的矩阵.

You can verify for yourself that these indices correspond to the correct elements in T to create your desired matrix in this case.

我们最终使用它来索引矩阵以获取正确的元素:

We finally use this to index into our matrix to grab the right elements:

out = T(ind);

X = 1;W = 3;执行此操作将得出:

Doing this for X = 1; and W = 3; gives:

>> out = T(ind)

out =

     1     5     6
     5     6     8
     6     8    10
     8    10    14
    10    14    22

类似地,对于X = 2;W = 3;给出:

>> out = T(ind)

out =

     1     5     6
     6     8    10
    10    14    22

这篇关于带有滑动窗口元素的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆