[R] multiplying a matrix by a vector

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Nov 4 16:41:52 CET 2016


Sara wins on memory use.

Rui wins on speed.

Bert wins on clarity.

library(microbenchmark)

N <- 1000
x <- matrix( runif( N*N ), ncol=N )
y <- seq.int( N )

microbenchmark( { t( y * t(x) ) }
               , { x %*% diag( y ) }
               , { sweep( x, 2, y, `*` ) }
               )
Unit: milliseconds
                         expr       min        lq    median        uq      max neval
          {     t(y * t(x)) }  6.659562  7.475414  7.871341  8.182623 47.01105 100
        {     x %*% diag(y) }  9.859292 11.014021 11.281334 11.733825 48.79463 100
  {     sweep(x, 2, y, `*`) } 16.535938 17.682175 18.283572 18.712342 55.47159 100

On Fri, 4 Nov 2016, Dimitri Liakhovitski wrote:

> Nice!
> Thanks a lot, everybody!
> Dimitri
>
> On Fri, Nov 4, 2016 at 10:35 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
>> My goodness!
>>
>>> x %*% diag(y)
>>
>>      [,1] [,2]
>> [1,]    2   12
>> [2,]    4   15
>> [3,]    6   18
>>
>> will do.
>>
>> -- Bert
>>
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>> On Thu, Nov 3, 2016 at 2:33 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
>>> Like this?
>>>
>>>> sweep(x, 2, y, "*")
>>>      [,1] [,2]
>>> [1,]    2   12
>>> [2,]    4   15
>>> [3,]    6   18
>>>>
>>>
>>>
>>> On Thu, Nov 3, 2016 at 5:05 PM, Dimitri Liakhovitski
>>> <dimitri.liakhovitski at gmail.com> wrote:
>>>> Hello!
>>>>
>>>> I have a matrix x and a vector y:
>>>>
>>>> x <- matrix(1:6, ncol = 2)
>>>> y <- c(2,3)
>>>>
>>>> I need to multiply the first column of x by 2 (y[1]) and the second
>>>> column of x by 3 (y[2]).
>>>>
>>>> Of course, I could do this - but it's column by column:
>>>>
>>>> x[,1] <- x[,1] * y[1]
>>>> x[,2] <- x[,2] * y[2]
>>>> x
>>>>
>>>> Or I could repeat each element of y and multiply two matrices - that's better:
>>>>
>>>> rep.row<-function(x,n){
>>>>   matrix(rep(x,each=n),nrow=n)
>>>> }
>>>> y <- rep.row(y, nrow(x))
>>>> x * y
>>>>
>>>> However, maybe there is a more elegant r-like way of doing it?
>>>> Thank you!
>>>>
>>>> --
>>>> Dimitri Liakhovitski
>>>>
>>>
>>> --
>>> Sarah Goslee
>>> http://www.functionaldiversity.org
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>
>
>
> -- 
> Dimitri Liakhovitski
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

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



More information about the R-help mailing list