Multiply rows of matrix by vector?
Asked Answered
C

6

79

I have a numeric matrix with 25 columns and 23 rows, and a vector of length 25. How can I multiply each row of the matrix by the vector without using a for loop?

The result should be a 25x23 matrix (the same size as the input), but each row has been multiplied by the vector.

Added reproducible example from @hatmatrix's answer:

matrix <- matrix(rep(1:3,each=5),nrow=3,ncol=5,byrow=TRUE)

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

vector <- 1:5

Desired output:

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    4    6    8   10
[3,]    3    6    9   12   15
Colcothar answered 4/9, 2010 at 18:51 Comment(1)
[These](<#32691349) solutions using outer() or collapse::TRA() are is significantly faster than anything suggested here.Cesspool
S
96

I think you're looking for sweep().

# Create example data and vector
mat <- matrix(rep(1:3,each=5),nrow=3,ncol=5,byrow=TRUE)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    2    2    2    2    2
[3,]    3    3    3    3    3

vec <- 1:5

# Use sweep to apply the vector with the multiply (`*`) function
#  across columns (See ?apply for an explanation of MARGIN) 
sweep(mat, MARGIN=2, vec, `*`)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    4    6    8   10
[3,]    3    6    9   12   15

It's been one of R's core functions, though improvements have been made on it over the years.

Smallminded answered 6/9, 2010 at 9:52 Comment(0)
D
48
> MyMatrix <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3, byrow=TRUE)
> MyMatrix
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]   11   12   13
> MyVector <- c(1:3)
> MyVector
[1] 1 2 3

You could use either:

> t(t(MyMatrix) * MyVector)
     [,1] [,2] [,3]
[1,]    1    4    9
[2,]   11   24   39

or:

> MyMatrix %*% diag(MyVector)
     [,1] [,2] [,3]
[1,]    1    4    9
[2,]   11   24   39
Digressive answered 4/9, 2010 at 19:6 Comment(0)
D
30

Actually, sweep is not the fastest option on my computer:

MyMatrix <- matrix(c(1:1e6), ncol=1e4, byrow=TRUE)
MyVector <- c(1:1e4)

Rprof(tmp <- tempfile(),interval = 0.001)
t(t(MyMatrix) * MyVector) # first option
Rprof()
MyTimerTranspose=summaryRprof(tmp)$sampling.time
unlink(tmp)

Rprof(tmp <- tempfile(),interval = 0.001)
MyMatrix %*% diag(MyVector) # second option
Rprof()
MyTimerDiag=summaryRprof(tmp)$sampling.time
unlink(tmp)

Rprof(tmp <- tempfile(),interval = 0.001)
sweep(MyMatrix ,MARGIN=2,MyVector,`*`)  # third option
Rprof()
MyTimerSweep=summaryRprof(tmp)$sampling.time
unlink(tmp)

Rprof(tmp <- tempfile(),interval = 0.001)
t(t(MyMatrix) * MyVector) # first option again, to check order 
Rprof()
MyTimerTransposeAgain=summaryRprof(tmp)$sampling.time
unlink(tmp)

MyTimerTranspose
MyTimerDiag
MyTimerSweep
MyTimerTransposeAgain

This yields:

> MyTimerTranspose
[1] 0.04
> MyTimerDiag
[1] 40.722
> MyTimerSweep
[1] 33.774
> MyTimerTransposeAgain
[1] 0.043

On top of being the slowest option, the second option reaches the memory limit (2046 MB). However, considering the remaining options, the double transposition seems a lot better than sweep in my opinion.


Edit

Just trying smaller data a repeated number of times:

MyMatrix <- matrix(c(1:1e3), ncol=1e1, byrow=TRUE)
MyVector <- c(1:1e1)
n=100000

[...]

for(i in 1:n){
# your option
}

[...]

> MyTimerTranspose
[1] 5.383
> MyTimerDiag
[1] 6.404
> MyTimerSweep
[1] 12.843
> MyTimerTransposeAgain
[1] 5.428
Digressive answered 21/9, 2010 at 19:46 Comment(2)
In my experience, if you throw a bunch of NAs into the matrix, the time taken by diag seems to go through the roof. For a 1E4x1E4 mat containing 1E5 NAs, I obtain: MyTimerTranspose=0.014, MyTimerSweep=0.042, MyTimerDiag=67.738. I'd replicate, but I'm impatient... just something to keep in mind.Coucher
I really like the double transposition answer, mainly because it shows what the answer is if we replace "row" with "column", making the answer the trivial A*x, which isn't obvious unless you truly understand how R works with matrices.Buckingham
T
6

For speed one may create matrix from the vector before multiplying

mat <-  matrix(rnorm(1e6), ncol=1e4)
vec <- c(1:1e4)
mat * matrix(vec, dim(mat)[1], length(vec))

library(microbenchmark)
microbenchmark(
  transpose = t(t(mat) * vec), 
  make_matrix = mat * matrix(vec, dim(mat)[1], length(vec), byrow = TRUE),
  sweep = sweep(mat,MARGIN=2,vec,`*`))
#Unit: milliseconds
#       expr      min        lq     mean    median       uq      max neval cld
#  transpose 9.940555 10.480306 14.39822 11.210735 16.19555 77.67995   100   b
#make_matrix 5.556848  6.053933  9.48699  6.662592 10.74121 74.14429   100   a 
#      sweep 8.033019  8.500464 13.45724 12.331015 14.14869 77.00371   100   b
Tia answered 1/12, 2018 at 14:38 Comment(0)
B
2

If you want speed, you can use Rfast::eachrow. It is the fastest from all...

Bencher answered 20/12, 2020 at 1:35 Comment(0)
M
1

Here is another option:

X <- matrix(rep(1:3, 5), nrow = 3)
v <- 1:5

X * v[col(X)]

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    4    6    8   10
[3,]    3    6    9   12   15

This works for any operation, not just multiplication.

Mailemailed answered 9/4, 2023 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.