计算两个矩阵之间的列式点积 [英] Calculating column-wise dot product between two matrices

查看:322
本文介绍了计算两个矩阵之间的列式点积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定一个矩阵中的第 i 列与第二矩阵中的 i 列之间的点积.最终结果将是一个包含 i 点积的数组.

I would like to determine the dot product between the ith column in one matrix and the ith column in a second matrix. The end result will be an array containing i dot products.

以下是我想做的可复制示例:

Here is a reproducible example of what I want to do:

#Generate two matrices with 10 columns each with 5 rows (thus i in this case is 10)
m1 <- replicate(10, rnorm(5)) 
m2 <- replicate(10, rnorm(5)) 

#Inefficiently calculate dot product of ith column in m1 with ith column in m2
d1 <- t(m1[,1]) %*% m2[,1]
d2 <- t(m1[,2]) %*% m2[,2]
...
d10 <- t(m1[,10]) %*% m2[,10]

#End with all dot products in array
d.final <- c(d1, d2, d10)

这里的任何帮助将不胜感激!

Any help here will be appreciated!

推荐答案

以下是一些选择

最清晰的选择(imo)是使用循环

The clearest option (imo) is to use a loop

out1 <- sapply(1:10, function(i) crossprod(m1[,i], m2[,i]))

或计算完整的矩阵叉积

out2 <- diag(crossprod(m1, m2))

或通过将元素乘法相加

out3 <- colSums(m1*m2)

检查

all.equal(out1, out2)
all.equal(out3, out2)


一些基准


A little benchmark

f1 <- function() sapply(1:ncol(m1), function(i) crossprod(m1[,i], m2[,i]))
f2 <- function() diag(crossprod(m1, m2))
f3 <- function() colSums(m1*m2)

# on your data
microbenchmark::microbenchmark(f1(), f2(), f3())
#  Unit: microseconds
#  expr    min      lq     mean median      uq     max neval cld
#  f1() 62.508 65.2325 69.46337 66.873 70.9945 123.271   100   b
#  f2()  8.312  9.4290 12.05529  9.778 10.2670 229.708   100   a 
#  f3() 11.385 12.4325 16.14248 12.921 13.5500 310.235   100   a 

# on bigger data
m1 <- replicate(1000, rnorm(1000)) 
m2 <- replicate(1000, rnorm(1000)) 
all.equal(f1(), f2())
all.equal(f2(), f3())

microbenchmark::microbenchmark(f1(), f3()) #f2 took too long
#  Unit: milliseconds
#  expr       min        lq     mean   median       uq      max neval cld
#  f1() 35.181220 38.065454 41.59141 39.93301 42.17569 82.13914   100   b
#  f3()  8.349807  9.220799 11.13763 10.19330 11.21829 53.57050   100   a 

这篇关于计算两个矩阵之间的列式点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆