I think you need something along these lines: ind <- c(sapply(1:(K+1), seq, length = K + 1)) cf1 <- rep(rep(coef, each = n), K + 1) cf2 <- rep(rep(coef, each = n), each = K + 1) rowSums(cf1 * cf2 * U[, ind])
I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Ravi Varadhan" <[EMAIL PROTECTED]> To: <r-help@stat.math.ethz.ch> Sent: Tuesday, November 14, 2006 4:45 PM Subject: [R] Matrix-vector multiplication without loops > Hi, > > > > 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? > > > > Thank you, > > Ravi. > > ---------------------------------------------------------------------------- > ------- > > Ravi Varadhan, Ph.D. > > Assistant Professor, The Center on Aging and Health > > Division of Geriatric Medicine and Gerontology > > Johns Hopkins University > > Ph: (410) 502-2619 > > Fax: (410) 614-9625 > > Email: [EMAIL PROTECTED] > > Webpage: > http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html > > > > ---------------------------------------------------------------------------- > -------- > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm ______________________________________________ R-help@stat.math.ethz.ch 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.