[R] Faster matrix operation?

Remko Duursma remkoduursma at gmail.com
Tue Jun 1 08:03:50 CEST 2010

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)?


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

Mobile: +61 (0)422 096908

More information about the R-help mailing list