[R] Matrix-vector multiplication without loops

Christos Hatzis christos at nuverabio.com
Tue Nov 14 20:48:56 CET 2006


Ravi,

Here is another version, somewhat similar to what Dimitris proposed but
without using 'rep'.  For large n, K 'rep' tries allocating two vectors,
each of length n*(K+1)^2, which can be a problem.  In this version 'outer'
buys you some efficiency and compactness, but your looping version is
faster.

n <- 1000
K <- 1000
p <- rep(0, n)
cf <- runif(K+1)
U <- matrix(runif(n*(2*K+1)), n, 2*K+1)

# original code
system.time(
{
    for (i in 0:K)
        for (j in 0:K)
            p <- p + cf[i+1]* cf[j+1] * U[,i+j+1]
    p.1 <- p
} )

# 'vectorized'
system.time(
{
    ind <- sapply(1:(K+1), seq, length = K+1)
    cc <- outer(cf,cf)
    p.2 <- apply(U, 1, FUN=function(u) sum(cc * u[ind]))
} )


all.equal(p.1, p.2)
rm(n,K,p,U,cf,cc,ind,p.1,p.2) 

-Christos

Christos Hatzis, Ph.D.
Nuvera Biosciences, Inc.
400 West Cummings Park
Suite 5350
Woburn, MA 01801
Tel: 781-938-3830
www.nuverabio.com
 


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Dimitris Rizopoulos
Sent: Tuesday, November 14, 2006 11:20 AM
To: Ravi Varadhan
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Matrix-vector multiplication without loops

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" <rvaradhan at jhmi.edu>
To: <r-help at 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: rvaradhan at jhmi.edu
>
> Webpage: 
> http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
>
>
>
>
----------------------------------------------------------------------------
> --------
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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 at 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.



More information about the R-help mailing list