MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   Matlab用户在R中的窗口特征提取 (https://www.labfans.com/bbs/showthread.php?t=26537)

poster 2019-12-14 20:13

Matlab用户在R中的窗口特征提取
 
我开始用R编程(来自Matlab),我只是想知道是否有一个类似于Matlab中buffer.m的内置R函数,请参见: [URL="http://www.mathworks.com/help/toolbox/signal/buffer.html"]http[/URL] : [URL="http://www.mathworks.com/help/toolbox/signal/buffer.html"]//www.mathworks.com/help/toolbox/signal/ buffer.html[/URL]

我目前是自己实现的,但是内置函数可能还是有用/更快的。



[B]回答:[/B]

Matlab的buffer命令可以使用rollapply表示。以下一个内衬给出与buffer(x, n, p)相同的结果buffer(x, n, p)并且似乎适用于非负p:

> library(zoo) > x n p unname(t(rollapply(zoo(c(rep(0, p), x, rep(0, np))), n, by = np, c))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 0 2 6 10 14 18 22 26 [2,] 0 3 7 11 15 19 23 27 [3,] 0 4 8 12 16 20 24 28 [4,] 1 5 9 13 17 21 25 29 [5,] 2 6 10 14 18 22 26 30 [6,] 3 7 11 15 19 23 27 0 [7,] 4 8 12 16 20 24 28 0 在Zoo的开发版本中,可以将其略微缩短为:

t(rollapply(c(rep(0, p), x, rep(0, np)), n, by = np, c)) 编辑:

请注意,首次回答问题时,zoo的开发版本已成为CRAN上Zoo的生产版本,因此现在可以使用较短的第二版代码。



[url=https://stackoverflow.com/questions/5238849]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 14:53

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.