用MATLAB函数处理带有滑动窗口功能的矢量,矢量响应的返回矩阵 [英] MATLAB function to process vector with sliding window function, return matrix of vector responses

查看:276
本文介绍了用MATLAB函数处理带有滑动窗口功能的矢量,矢量响应的返回矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定大小为1 x n的向量v和函数fun接受长度为L的向量并返回大小为p x 1的向量.

Assuming vector v of size 1 x n and function fun that takes in a vector of length L and returns a vector of size p x 1.

是否有一个MATLAB函数可以接收向量v,并使用fun函数处理长度为L的每个滑动窗口,并返回大小为p x n(或p x (n-L))的矩阵.

Is there a MATLAB function that would take in vector v, process each sliding window of length L with function fun, and return a matrix of size p x n (or p x (n-L)).

我知道这可以通过使用im2col创建窗口矢量矩阵并对其进行处理来实现,但这对于长矢量v来说会占用太多内存.

I am aware this could be achieved with creating a matrix of windowed vectors with im2col and processing each of those, but this takes too much memory for a long vector v.

推荐答案

funsl=@(is) fun(v(is:is+l-1));
cell2mat(arrayfun(funsl,1:length(v)-l+1,'UniformOutput',false))

我在这里所做的是定义一个匿名函数,对于固定的vl和起始索引参数(is),获取相应的v切片并将fun应用于该函数

What I did here is define an anonymous function that, for a fixed v and l and a starting index parameter (is), gets the respective slice of v and applies fun to it.

然后通过arrayfun将此函数应用于该起始索引的所有有用值.出于某种原因,我本人目前无法准确命名,每个应用程序都返回一个p x 1向量,但是arrayfun无法将其排列成适当的矩阵,因此UniformOutput = false设置和cell2mat围绕它进行调用.

Then this function is applied, via arrayfun, to all useful values for this starting index. For reasons I myself cannot quite name at the moment, each application returns a p x 1 vector, but arrayfun cannot arrange it into a proper matrix, thus the UniformOutput=false setting and the cell2mat call around it.

编辑:要使用将1×5矢量转换为我使用的4×1矢量的功能进行测试

Edit: To test this with a function that turns 1-by-5 vectors into 4-by-1 vectors I used

l=5;v=1:12; fun=@(x) cumsum(x(2:end))';

并得到以下结果:

ans =

 2     3     4     5     6     7     8     9
 5     7     9    11    13    15    17    19
 9    12    15    18    21    24    27    30
14    18    22    26    30    34    38    42

再次注意,在funsl v的定义中是固定的,要将这种方法应用于不同的v,您可以制作另一个采用v(和l)的函数,如果您不想这样做的话.将其修复为参数,包含上面的两行并返回第二行的结果.

Note again that in the definition of funsl v is fixed, To apply this approach to different v you could make another function that takes v (and l, if you do not want to fix this) as parameter(s), contains the two lines above and returns the result of the second one.

这篇关于用MATLAB函数处理带有滑动窗口功能的矢量,矢量响应的返回矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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