简体   繁体   中英

Efficient row wise matrix operation in R

I have 2 matrices M1, M2. For each row in M1, I want to find the maximum value of the product of that row in M1 and each row in M2.

I have tried the following implementation which produces the result I want.

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

This takes 16 seconds on my machine. Is there a faster implementation? Thanks!

Running in parallel gives an easier speed. On my machine, the serial version is 15 seconds, the parallel version is just under 4 seconds.

Load the package

# Comes with R
library(parallel)

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

Then we need to explicitly export M2

clusterExport(cl, "M2")

and run as normal

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

Using a for loop gives quite a speed up for me

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM