Faster matrix operation?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Faster matrix operation?

Remko Duursma
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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Faster matrix operation?

Bill.Venables
> 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: [hidden email] [mailto:[hidden email]] On Behalf Of Remko Duursma
Sent: Tuesday, 1 June 2010 4:04 PM
To: [hidden email]
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

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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Faster matrix operation?

Peter Ehlers
On 2010-06-01 0:16, [hidden email] wrote:

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

Or, for a very slight further reduction in time in
the case of larger matrices/vectors:

  as.vector(tcrossprod(V, xyzs))

I mention this merely to remind new users of the
excellent speed of [t]crossprod().

  -Peter Ehlers

>>
> ?
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Remko Duursma
> Sent: Tuesday, 1 June 2010 4:04 PM
> To: [hidden email]
> 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
>
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Faster matrix operation?

baptiste auguie-5
On 1 June 2010 11:34, Peter Ehlers <[hidden email]> wrote:
> Or, for a very slight further reduction in time in
> the case of larger matrices/vectors:
>
>  as.vector(tcrossprod(V, xyzs))
>
> I mention this merely to remind new users of the
> excellent speed of [t]crossprod().
>
>  -Peter Ehlers

Thanks, I've been using t(x)%*%y for several years now, and had never
found out there was a faster and more straight-forward function for
this. Perhaps a link to [t]crossprod could be added on the help page
for ?"%*%".

Best,

baptiste

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