如果我有一个(批量x 5)矩阵和一个(1 x 5)矩阵,有没有比这更好的方法将它们乘以行:
> 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
这有效但似乎很糟糕。有没有我失踪的功能?
答案 0 :(得分:5)
你可以尝试
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
或者
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
或者
z*rep(q,each=ncol(z))
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
答案 1 :(得分:5)
另一个选项是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