矩阵的行乘以向量?

[英]Multiply rows of matrix by vector?


I'm optimizing a function and I want to get rid of slow for loops. I'm looking for a faster way to multiply each row of a matrix by a vector.

我在优化一个函数,我想摆脱循环的缓慢。我在寻找一种更快的方法将矩阵的每一行乘以一个向量。

Any ideas?

什么好主意吗?

EDIT:

编辑:

I'm not looking for a 'classical' multiplication.

我不是在寻找“经典”乘法。

Eg. I have matrix that has 23 columns and 25 rows and a vector that has length of 23. In a result I want to have matrix 25x23 that has each row multiplied by vector.

如。我有一个有23列25行的矩阵和一个长度为23的向量。结果,我想要一个矩阵25x23每一行乘以一个向量。

4 个解决方案

#1


62  

I think you're looking for sweep().

我想你是在找横扫()。

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

这是R的核心功能之一,尽管多年来已经有了改进。

#2


33  

> 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

#3


22  

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.

除了是最慢的选项之外,第二个选项达到内存限制(2046 MB)。然而,在我看来,考虑到其他的选择,双置换比清除要好得多。


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

#4


-2  

Google "R matrix multiplcation" yields Matrix Multiplication, which describes the %*% operator and says "Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors it will return the inner product (as a matrix)."

谷歌“R矩阵多重化”产生矩阵乘法,矩阵乘法描述了%*%的运算符,并说“如果两个矩阵是可整合的,则相乘”。如果一个参数是一个向量,它将被提升到一个行或列矩阵,使两个参数一致。如果两个都是向量,它会返回内积(作为一个矩阵)

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/09/04/e39a208c5e975878a3279d32541ffd8c.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告