Avoid two for loops in R

前端 未结 6 757
执念已碎
执念已碎 2021-02-08 11:15

I have a R code that can do convolution of two functions...

convolveSlow <- function(x, y) {  
nx <- length(x); ny <- length(y)  
xy <- numeric(nx          


        
6条回答
  •  悲哀的现实
    2021-02-08 11:48

    How about convolve(x, rev(y), type = "open") in stats?

    > x <- runif(1000)
    > y <- runif(1000)
    > system.time(a <- convolve(x, rev(y), type = "o"))
       user  system elapsed 
      0.032   0.000   0.032 
    > system.time(b <- convolveSlow(x, y))
       user  system elapsed 
     11.417   0.060  11.443 
    > identical(a,b)
    [1] FALSE
    > all.equal(a,b)
    [1] TRUE
    

提交回复
热议问题