R中的矩阵和向量乘法运算

2023-12-25

我觉得 R 中的矩阵运算非常令人困惑:我们混合了行向量和列向量。

  • 这里我们定义x1作为向量,(我假设 R 默认向量是列向量?但它没有显示它是这样排列的。)

  • 然后我们定义x2是一个转置x1,这个显示对我来说也很奇怪。

  • 最后,如果我们定义x3作为矩阵,显示效果似乎更好。

现在,我的问题是,x1 and x2是完全不同的东西(一个是另一个的转置),但我们在这里得到相同的结果。

有什么解释吗?也许我不应该将向量和矩阵运算混合在一起?

x1 = c(1:3)
x2 = t(x1)
x3 = matrix(c(1:3), ncol = 1)

x1
[1] 1 2 3

x2
     [,1] [,2] [,3]
[1,]    1    2    3

x3
     [,1]
[1,]    1
[2,]    2
[3,]    3

x3 %*% x1
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9

x3 %*% x2
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9

See ?`%*%`:

描述:

 Multiplies two matrices, if they are conformable.  If one argument
 is a vector, it will be promoted to either a row or column matrix
 to make the two arguments conformable.  If both are vectors of the
 same length, it will return the inner product (as a matrix).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R中的矩阵和向量乘法运算 的相关文章

随机推荐