我觉得 R 中的矩阵运算非常困惑:我们混合了行向量和列向量。
这里我们将
x1
定义为一个向量,(我假设R默认向量是一个列向量?但它并没有显示它是那样排列的。)然后我们定义
x2
是x1
的转置,我觉得显示也很奇怪。最后,如果我们将
x3
定义为矩阵,显示效果会更好。
现在,我的问题是,x1
和 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
最佳答案
参见 ?`%*%`
:
Description:
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).
关于r - R中的矩阵和向量乘法运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39025900/