Matrix max by row
Bert Gunter wrote:
Serves me right, I suppose. Timing seems also very dependent on the dimensions of the matrix. Here's what I got with my inadequate test:
x <- matrix(rnorm(3e5),ncol=3)
## via apply
system.time(apply(x,1,max))
user system elapsed 2.09 0.02 2.10 ## via pmax
system.time(do.call(pmax,data.frame(x)))
user system elapsed 0.10 0.02 0.11
yes, similar to what i got. but with the transpose, the ratio is way
more than inverted:
waku = expression(matrix(apply(m, 1, max), nrow(m)))
bert = expression(do.call(pmax, data.frame(m)))
library(rbenchmark)
m = matrix(rnorm(1e6), ncol=10)
benchmark(replications=10, columns=c('test', 'elapsed'),
order='elapsed',
waku=waku,
bert=bert)
# test elapsed
# 2 bert 1.633
# 1 waku 9.974
m = t(m)
benchmark(replications=10, columns=c('test', 'elapsed'),
order='elapsed',
waku=waku,
bert=bert)
# test elapsed
# 1 waku 0.507
# 2 bert 27.261
Draw your own conclusions!
my favourite: you should have specified what 'large matrices' means. vQ