Skip to content
Prev 175407 / 398506 Next

Matrix max by row

It seems to be very system dependent.  Here's another take:
user  system elapsed 
   1.53    0.01    1.57
user  system elapsed 
   1.81    0.00    1.83
Now what happens if you work with data frames rather than matrices:
user  system elapsed 
   0.31    0.00    0.31
user  system elapsed 
   3.22    0.03    3.34
Go figure!  


Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Rolf Turner
Sent: Monday, 30 March 2009 1:39 PM
To: Bert Gunter
Cc: 'Wacek Kusnierczyk'; r-help at r-project.org
Subject: Re: [R] Matrix max by row

I tried the following:

m <- matrix(runif(100000),1000,100)
junk <- gc()
print(system.time(for(i in 1:100) X1 <- do.call(pmax,data.frame(m))))
junk <- gc()
print(system.time(for(i in 1:100) X2 <- apply(m,1,max)))

and got

    user  system elapsed
   2.704   0.110   2.819
    user  system elapsed
   1.938   0.098   2.040

so unless there's something that I am misunderstanding (always a serious
consideration) Wacek's apply method looks to be about 1.4 times  
*faster* than
the do.call/pmax method.

	cheers,

		Rolf Turner
On 30/03/2009, at 3:55 PM, Bert Gunter wrote:

            
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

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