Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:将矩阵的行相乘_R_Matrix - Fatal编程技术网

R:将矩阵的行相乘

R:将矩阵的行相乘,r,matrix,R,Matrix,我有两个矩阵 B <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5) C <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5) 还是这样 apply(B*C, 1, sum) 他们俩都不快。有没有一种惯用的方法可以更快地做到这一点 最后,我想改进下面的代码 beta_numerator <-

我有两个矩阵

B <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5)
C <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5)
还是这样

apply(B*C, 1, sum)
他们俩都不快。有没有一种惯用的方法可以更快地做到这一点

最后,我想改进下面的代码

beta_numerator <- matrix(0,K,V)
for(k in (1:K)){
  for(i in (1:V)){
    beta_numerator[k,i] <- crossprod(m_tf[i,], gamma[i,,k])
  }
}

beta_分子Gregor的行和解将是fastet。但是,如果您编译for循环的字节码(在apply中不会同样好用),您将获得类似的速度—而且不需要花费太多精力:

B <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5)
C <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5)

# I want to calculate crossprod of rows of those matrices. Currently I am doing it this way:
for.fun <- function (B, C) {
  res <- numeric(nrow(B))
  for (i in seq_along(res)) {
    res[i] <- B[i, ] %*% C[i, ]
  }
  res
}
for.fun.compiled <- compiler::cmpfun(for.fun)

# or this way
apply.fun <- function(B, C) {
  apply(B*C, 1, sum) 
}
apply.fun.compiled <- compiler::cmpfun(apply.fun)

# rowSums
row.fun <- function(B, C) {
  rowSums(B * C)
}
row.fun.compiled <- compiler::cmpfun(row.fun)

library(microbenchmark)
microbenchmark(
  apply.fun (B, C),
  apply.fun.compiled (B, C),
  for.fun(B, C),
  for.fun.compiled (B, C),
  row.fun(B, C),
  row.fun.compiled(B, C)
)

# result:   
# Unit: microseconds
#                     expr    min     lq     mean  median      uq      max neval cld
#           apply.fun(B, C) 20.081 20.528 40.24717 20.9740 21.4200 1758.209   100   a
# apply.fun.compiled(B, C) 19.635 20.528 21.95117 20.9735 21.4200   39.270   100   a
#            for.fun(B, C)  4.909  5.801 45.02650  6.2480  6.6940 3880.108   100   a
#   for.fun.compiled(B, C)  4.909  5.801  6.66713  6.2475  6.6935   19.189   100   a
#            row.fun(B, C)  4.016  4.909 19.55925  5.8010  5.8020 1391.395   100   a
#   row.fun.compiled(B, C)  4.016  4.463  5.51139  5.3550  5.8020   17.850   100   a

BGregor的行和解决方案将是fastet。但是,如果您编译for循环的字节码(在apply中不会同样好用),您将获得类似的速度—而且不需要花费太多精力:

B <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5)
C <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5)

# I want to calculate crossprod of rows of those matrices. Currently I am doing it this way:
for.fun <- function (B, C) {
  res <- numeric(nrow(B))
  for (i in seq_along(res)) {
    res[i] <- B[i, ] %*% C[i, ]
  }
  res
}
for.fun.compiled <- compiler::cmpfun(for.fun)

# or this way
apply.fun <- function(B, C) {
  apply(B*C, 1, sum) 
}
apply.fun.compiled <- compiler::cmpfun(apply.fun)

# rowSums
row.fun <- function(B, C) {
  rowSums(B * C)
}
row.fun.compiled <- compiler::cmpfun(row.fun)

library(microbenchmark)
microbenchmark(
  apply.fun (B, C),
  apply.fun.compiled (B, C),
  for.fun(B, C),
  for.fun.compiled (B, C),
  row.fun(B, C),
  row.fun.compiled(B, C)
)

# result:   
# Unit: microseconds
#                     expr    min     lq     mean  median      uq      max neval cld
#           apply.fun(B, C) 20.081 20.528 40.24717 20.9740 21.4200 1758.209   100   a
# apply.fun.compiled(B, C) 19.635 20.528 21.95117 20.9735 21.4200   39.270   100   a
#            for.fun(B, C)  4.909  5.801 45.02650  6.2480  6.6940 3880.108   100   a
#   for.fun.compiled(B, C)  4.909  5.801  6.66713  6.2475  6.6935   19.189   100   a
#            row.fun(B, C)  4.016  4.909 19.55925  5.8010  5.8020 1391.395   100   a
#   row.fun.compiled(B, C)  4.016  4.463  5.51139  5.3550  5.8020   17.850   100   a
B
rowSums(B*C)
将是
apply(B*C,1,sum)
]的更快版本。看起来快了3到4倍,但是相乘列-Gregors建议是一个很好的建议
rowSums(B*C)
将是
apply(B*C,1,sum)
]的更快版本。看起来要快3到4倍,但是乘以列-Gregors的建议是一个很好的建议
B <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5)
C <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15), nrow = 3, ncol = 5)

# I want to calculate crossprod of rows of those matrices. Currently I am doing it this way:
for.fun <- function (B, C) {
  res <- numeric(nrow(B))
  for (i in seq_along(res)) {
    res[i] <- B[i, ] %*% C[i, ]
  }
  res
}
for.fun.compiled <- compiler::cmpfun(for.fun)

# or this way
apply.fun <- function(B, C) {
  apply(B*C, 1, sum) 
}
apply.fun.compiled <- compiler::cmpfun(apply.fun)

# rowSums
row.fun <- function(B, C) {
  rowSums(B * C)
}
row.fun.compiled <- compiler::cmpfun(row.fun)

library(microbenchmark)
microbenchmark(
  apply.fun (B, C),
  apply.fun.compiled (B, C),
  for.fun(B, C),
  for.fun.compiled (B, C),
  row.fun(B, C),
  row.fun.compiled(B, C)
)

# result:   
# Unit: microseconds
#                     expr    min     lq     mean  median      uq      max neval cld
#           apply.fun(B, C) 20.081 20.528 40.24717 20.9740 21.4200 1758.209   100   a
# apply.fun.compiled(B, C) 19.635 20.528 21.95117 20.9735 21.4200   39.270   100   a
#            for.fun(B, C)  4.909  5.801 45.02650  6.2480  6.6940 3880.108   100   a
#   for.fun.compiled(B, C)  4.909  5.801  6.66713  6.2475  6.6935   19.189   100   a
#            row.fun(B, C)  4.016  4.909 19.55925  5.8010  5.8020 1391.395   100   a
#   row.fun.compiled(B, C)  4.016  4.463  5.51139  5.3550  5.8020   17.850   100   a