R row multiplication - restyle?

If I have, say, a matrix (many x 5) and a matrix (1 x 5), there is a better way to multiply them row by row than this:

> q 
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5

> z
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25

> t(apply(z,1,function (x) {x*q})) 
     [,1] [,2] [,3] [,4] [,5]
[1,]    1   12   33   64  105
[2,]    2   14   36   68  110
[3,]    3   16   39   72  115
[4,]    4   18   42   76  120
[5,]    5   20   45   80  125

      

It works, but it seems bad. Is there a feature I am missing?

+3


source to share


2 answers


Another variant: sweep



sweep(z, 2, q, "*")
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1   12   33   64  105
# [2,]    2   14   36   68  110
# [3,]    3   16   39   72  115
# [4,]    4   18   42   76  120
# [5,]    5   20   45   80  125

      

+5


source


You may try

z*q[col(z)]
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    1   12   33   64  105
#[2,]    2   14   36   68  110
#[3,]    3   16   39   72  115
#[4,]    4   18   42   76  120
#[5,]    5   20   45   80  125

      

or

t(t(z)*c(q))
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    1   12   33   64  105
#[2,]    2   14   36   68  110
#[3,]    3   16   39   72  115
#[4,]    4   18   42   76  120
#[5,]    5   20   45   80  125

      



or

z*rep(q,each=ncol(z))

      

Benchmarks

z <- matrix(1:5e6,ncol=5)
f1 <- function() {sweep(z, 2, q, "*")}
f2 <- function() {z*q[col(z)]}
f3 <- function() {z*rep(q,each=ncol(z))}
library(microbenchmark)
microbenchmark(f1(), f2(),f3(), unit='relative', times=40L)
# Unit: relative
#expr      min       lq     mean   median       uq      max neval cld
#f1() 4.411211 4.407288 4.370018 4.308901 4.825270 2.396968    40   c
#f2() 2.607341 2.668707 2.916354 3.323934 3.321174 1.437837    40  b 
#f3() 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000    40 a  

      

+5


source







All Articles