Skip to content
Back to formatted view

Raw Message

Message-ID: <49D1262D.2070001@idi.ntnu.no>
Date: 2009-03-30T20:06:05Z
From: Wacek Kusnierczyk
Subject: Matrix max by row
In-Reply-To: <001301c9b14b$a2e99390$3a0b2c0a@gne.windows.gene.com>

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