[R] Avoiding two loops

Dirk Eddelbuettel edd at debian.org
Fri Feb 4 20:14:36 CET 2011


On 4 February 2011 at 14:03, sudhir cr wrote:
| Hello,
| 
| I have a R code for doing convolution of two functions:
| 
| convolveSlow <- function(x, y) {
|     nx <- length(x); ny <- length(y)
|     xy <- numeric(nx + ny - 1)
|     for(i in seq(length = nx)) {
|         xi <- x[[i]]
|         for(j in seq(length = ny)) {
|             ij <- i+j-1
|             xy[[ij]] <- xy[[ij]] + xi * y[[j]]
|         }
|     }
|     xy
| }
| 
| How do I reduce the 2 loops so that I can run the  code faster?

Maybe by reading the answer to the _exact same question_ you appear to have
asked on SO yesterday?

http://stackoverflow.com/questions/4894506/avoid-two-for-loops-in-r

Dirk

| 
| Thank you
| San
| 
| 	[[alternative HTML version deleted]]
| 
| ______________________________________________
| R-help at r-project.org mailing list
| https://stat.ethz.ch/mailman/listinfo/r-help
| PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
| and provide commented, minimal, self-contained, reproducible code.

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



More information about the R-help mailing list