On 11/14/06, Ravi Varadhan <[EMAIL PROTECTED]> wrote:
> I am trying to do the following computation:
>
>       p <- rep(0, n)
>       coef <- runif(K+1)
>       U <- matrix(runif(n*(2*K+1)), n, 2*K+1)
>       for (i in 0:K){
>       for (j in 0:K){
>       p <- p + coef[i+1]* coef[j+1] * U[,i+j+1]
>       } }
>
> I would appreciate any suggestions on how to perform this computation
> efficiently without the "for" loops?

This kicks butt on my machine:

p <- as.vector(U %*% convolve(coef,rev(coef),type="open"))

HTH!

Richard Graham
JHU '84 EE&CS

______________________________________________
[email protected] 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.

Reply via email to