Avoid two for loops in R

前端 未结 6 748
执念已碎
执念已碎 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:58

    1. For vectors, you index with [], not [[]], so use xy[ij] etc

    2. Convolution doesn't vectorise easily but one common trick is to switch to compiled code. The Writing R Extensions manual uses convolution as a running example and shows several alternative; we also use it a lot in the Rcpp documentation.

提交回复
热议问题