0
2つの行列AとBがあります。Aのサイズは2M * 50、Bは20k * 50です。私は各行のA%*%t(B)の上位10個の値を計算したいと思います。この1R - 行列積の高速ソート
library(parallel)
library(pbapply)
set.seed(1)
A <- matrix(runif(2e6*50), nrow=2e6)
B <- matrix(runif(2e4*50), nrow=2e4)
n <- 10
cl = makeCluster(detectCores())
clusterExport(cl, c("A","B", "n"))
Z <- pbsapply(1:nrow(A), function(x){
score = A[x,] %*% t(B)
nth_score = -sort(-score, partial=n)[n]
top_scores_1 = which(score > nth_score)
top_scores_2 = which(score == nth_score)
if (!length(top_scores_2) == 1) top_scores_2 = sample(top_scores_2, n - length(top_scores_1))
top_scores = c(top_scores_1, top_scores_2)
top_ix = sort(score[top_scores], decreasing = T, index.return=T)$ix
return(top_scores[top_ix])
}, cl = cl)
stopCluster(cl)