且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在R中将两个矩阵相乘

更新时间:2022-12-09 17:59:33

您需要对第二个矩阵进行转置才能获得所需的结果:

You need the transpose of the second matrix to get the result you wanted:

> v1 <- c(1,2,3)
> v2 <- matrix(c(3,1,2,2,1,3,3,2,1), ncol = 3, byrow = TRUE)
> v1 %*% t(v2)
     [,1] [,2] [,3]
[1,]   11   13   10

或者如果实际问题更大,则可能更快(请参阅?crossprod):

Or potentially quicker (see ?crossprod) if the real problem is larger:

> tcrossprod(v1, v2)
     [,1] [,2] [,3]
[1,]   11   13   10