Skip to content
Prev 175485 / 398506 Next

Matrix max by row

Bert Gunter wrote:
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
my favourite:  you should have specified what 'large matrices' means.

vQ