[R] matrix column division by vector

Bert Gunter gunter.berton at gene.com
Wed May 14 18:22:20 CEST 2014


Yes, but Jeff and I advocated essentially the same approach and the
difference between our versions is unimportant.

All the other approaches are at least aesthetically less desirable.

One "criticism" of our solution: it relies on the underlying
implementation of matrices rather than the matrix() API. In principle,
the implementation could change while the API remained constant.
However, the reality is that this would never happen (it would break
thousands of lines of code that use this approach because R was not in
the past and really still isn't entirely OO). But full disclosure
demands ...

Cheers,
Bert

-- Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch




On Wed, May 14, 2014 at 9:07 AM, David L Carlson <dcarlson at tamu.edu> wrote:
> Bert wins the race:
>
>> system.time(replicate(1e5, m/rep(v,e=2)))
>    user  system elapsed
>    0.25    0.00    0.25
>> system.time(replicate(1e5, m/matrix( v, ncol=ncol(m), nrow=nrow(m), byrow=TRUE)))
>    user  system elapsed
>    0.42    0.00    0.42
>> system.time(replicate(1e5, t(t(m)/v)))
>    user  system elapsed
>    1.31    0.00    1.33
>> system.time(replicate(1e5, sweep(m, 2, v, "/")))
>    user  system elapsed
>    3.39    0.00    3.40
>> system.time(replicate(1e5, t(apply(m, 1, function(x) x/v))))
>    user  system elapsed
>    5.04    0.01    5.06
>
> David C
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff Newmiller
> Sent: Wednesday, May 14, 2014 10:28 AM
> To: carol white; carol white; r-help at stat.math.ethz.ch
> Subject: Re: [R] matrix column division by vector
>
> Please post in plain text... your email is getting distorted and hard to read by the HTML.
>
> I don't know how to use do.call for this, but when you understand how vectors recycle and matrices and arrays are laid out in memory (read the Introduction to R document if not) then the following comes to mind:
>
> mat2 <- m / matrix( v, ncol=ncol(m), nrow=nrow(m), byrow=TRUE )
>
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                       Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> On May 14, 2014 7:51:36 AM PDT, carol white <wht_crl at yahoo.com> wrote:
>>Hi,
>>What is the elegant script to divide the columns of a matrix by the
>>respective position of a vector elements?
>>
>>m=rbind(c(6,4,2),c(3,2,1))
>>
>>v= c(3,2,1)
>>
>>res= 6/3�� 4/2� 2/1
>>������� 3/3�� 2/2 �� 1/1
>>
>>
>>this is correct�
>>mat2 = NULL
>>
>>for (i in 1: ncol(m))
>>
>>��� mat2 = cbind(mat2, m[,i]/ v[i])
>>
>>
>>but how to do more compact and elegant with for ex do.call?
>>
>>Many thanks
>>
>>Carol
>>       [[alternative HTML version deleted]]
>>
>>
>>
>>------------------------------------------------------------------------
>>
>>______________________________________________
>>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.
>
> ______________________________________________
> 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.
> ______________________________________________
> 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.



More information about the R-help mailing list