Re: [R] Faster matrix operation?

From: <Bill.Venables_at_csiro.au>
Date: Tue, 01 Jun 2010 16:16:25 +1000

> xyzs <- matrix(rnorm(3*100000,0,1),ncol=3)
>
> V <- c(2,3,4)
> system.time(vx <- apply(t(xyzs) * V, 2 ,sum))

   user system elapsed
   1.06 0.02 1.08
>
> system.time(wx <- as.vector(xyzs %*% V))

   user system elapsed

      0 0 0
> all.equal(vx, wx)

[1] TRUE
>

?

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Remko Duursma Sent: Tuesday, 1 June 2010 4:04 PM
To: r-help_at_r-project.org
Subject: [R] Faster matrix operation?

Dear R-helpers,

I have a three-column matrix with lots of rows:

xyzs <- matrix(rnorm(3*100000,0,1),ncol=3)

# And I am multiplying it with some vector V, and summing the rows (columns after t()) in this way:
V <- c(2,3,4)
system.time(vx <- apply(t(xyzs) * V, 2 ,sum))

Ok, this does not take long (0.9 sec on my machine), but I have to do this lots of times, with frequently larger matrices.

Is there a way to significantly speed this up, apart from writing it in Fortran or C and calling it from within R (which is what I am planning unless there is an alternative)?

thanks,
Remko



Remko Duursma
Research Lecturer

Centre for Plants and the Environment
University of Western Sydney
Hawkesbury Campus
Richmond NSW 2753

Dept of Biological Science
Macquarie University
North Ryde NSW 2109
Australia

Mobile: +61 (0)422 096908
www.remkoduursma.com



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.

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. Received on Tue 01 Jun 2010 - 06:21:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Jun 2010 - 09:50:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive