I'm trying to multiply a 5 x 3
matrix X
by its transpose tX
in R.
我试图将一个5 x 3矩阵X乘以它在R中的转置tX。
> X
[,1] [,2] [,3]
[1,] 1 13 0.5
[2,] 1 23 0.4
[3,] 1 7 -0.2
[4,] 1 16 1.0
[5,] 1 11 0.3
> tX
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0 1.0 1.0 1 1.0
[2,] 13.0 23.0 7.0 16 11.0
[3,] 0.5 0.4 -0.2 1 0.3
> tX * X
Error in tX * X: array incompatibili
I did this in MATLAB and got immediately the right result. Why can't I do this in R? Thank You.
我在MATLAB中做了这个,并立即得到了正确的结果。为什么我不能在R中这样做?谢谢。
在MATLAB中计算
1 个解决方案
#1
4
You need tX %*% X
. Or try crossprod(X)
. See ?"%*%"
.
你需要tX%*%X。或者尝试crossprod(X)。看?“%*%”。
Actually, if you just read the matrix-multiplication info, you will see this. I added it in two days ago.
实际上,如果你只是阅读矩阵乘法信息,你会看到这一点。我在两天前添加了它。
The "*"
is used for Hadamard product, i.e., the element-wise product. Since tX
and X
don't have the same dimension, you get error. (A hint: I don't know how you get tX
. In R, function t
transposes a matrix. See transpose info.)
“*”用于Hadamard产品,即元素产品。由于tX和X的维度不同,因此会出现错误。 (一个提示:我不知道你是怎么得到的。在R中,函数t转置一个矩阵。见转置信息。)
I haven't used MATLAB for 10 years. The following docs for R2018a looks different from what I recall.
我没有使用MATLAB 10年。 R2018a的以下文档与我记得的不同。
Anyway, the syntax between two scientific languages are different enough.
无论如何,两种科学语言之间的语法是不同的。
#1
4
You need tX %*% X
. Or try crossprod(X)
. See ?"%*%"
.
你需要tX%*%X。或者尝试crossprod(X)。看?“%*%”。
Actually, if you just read the matrix-multiplication info, you will see this. I added it in two days ago.
实际上,如果你只是阅读矩阵乘法信息,你会看到这一点。我在两天前添加了它。
The "*"
is used for Hadamard product, i.e., the element-wise product. Since tX
and X
don't have the same dimension, you get error. (A hint: I don't know how you get tX
. In R, function t
transposes a matrix. See transpose info.)
“*”用于Hadamard产品,即元素产品。由于tX和X的维度不同,因此会出现错误。 (一个提示:我不知道你是怎么得到的。在R中,函数t转置一个矩阵。见转置信息。)
I haven't used MATLAB for 10 years. The following docs for R2018a looks different from what I recall.
我没有使用MATLAB 10年。 R2018a的以下文档与我记得的不同。
Anyway, the syntax between two scientific languages are different enough.
无论如何,两种科学语言之间的语法是不同的。