R中的高效行式矩阵运算

时间:2017-05-28 19:34:42

标签: r performance matrix linear-algebra

我有2个矩阵M1,M2。对于M1中的每一行,我想找到M1中该行的乘积和M2中每行的最大值。

我尝试了以下实现,产生了我想要的结果。

set.seed(1)
st_time = Sys.time()
M1 = matrix(runif(1000*10), nrow=1000, ncol=10)
M2 = matrix(runif(10000*10), nrow=10000, ncol=10)

score = apply(M1, 1, function(x){
  w = M2 %*% diag(x)
  row_max = apply(w, 1, max)
  return(row_max)
})
required_output = t(score)
Sys.time() - st_time

我的机器需要16秒。有更快的实施吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

并行运行可以提高速度。在我的机器上,串行版本为15秒,并行版本不到4秒。

加载包

# Comes with R
library(parallel)

# Make the cluster 
# 8 cores, see detectCores() 
cl = makeCluster(8)

然后我们需要明确导出M2

clusterExport(cl, "M2")

正常运行

score_par = function() {
  parApply(cl, M1, 1, function(x){
    w = M2 %*% diag(x)
    row_max = apply(w, 1, max)
    return(row_max)
  })
}
system.time(score_par())

答案 1 :(得分:2)

使用for循环为我提供了相当快的速度

set.seed(1)
M1 = matrix(runif(1000*10), nrow=1000, ncol=10)
M2 = matrix(runif(10000*10), nrow=10000, ncol=10)

st_time = Sys.time()

tm = t(M2)
out = matrix(0, nr=nrow(M1), nc=nrow(M2))

for(i in 1:nrow(M1)){
  out[i, ] = matrixStats::colMaxs(M1[i, ]* tm)
}

Sys.time() - st_time
#Time difference of 1.835793 secs # was ~28secs with yours on my laptop


all.equal(required_output, out)
相关问题