将矩阵的行乘以矢量?


72

我有一个matrix包含25列和23行的数字,以及一个长度为25的向量。如何在不使用a的情况下将矩阵的每一行乘以向量for循环的?

结果应该是25x23的矩阵(与输入大小相同),但是每一行都已乘以向量。

从@hatmatrix的答案中添加了可复制的示例:

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

所需的输出:

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

Answers:


82

我认为您正在寻找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

它是R的核心功能之一,尽管多年来已经对其进行了改进。


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

您可以使用以下任一方法:

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

要么:

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

27

实际上,sweep这不是我计算机上最快的选择:

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

这样产生:

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

除了作为最慢的选项之外,第二个选项达到了内存限制(2046 MB)。但是,考虑到剩余的选项,双重换位似乎比sweep我认为的要好得多。


编辑

只是尝试多次较小的数据:

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

3
以我的经验,如果将一堆NAs放入矩阵中,花费的时间diag似乎要花很多时间。对于包含1E5NA的1E4x1E4垫,我获得:MyTimerTranspose= 0.014,MyTimerSweep= 0.042,MyTimerDiag= 67.738。我已经复制了,但是我不耐烦...只是要记住的一件事。
jbaums 2012年

我真的很喜欢双换位答案,主要是因为它显示了用“列”替换“行”时答案是什么,使答案变成平凡的A * x,除非您真正了解R如何处理矩阵,否则这个答案并不明显。 。
WetlabStudent 2013年

5

对于速度,可以在乘以之前从向量创建矩阵

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


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.