r里面对1维矩阵做下标操作会自动让一维矩阵变成vector,这样会对矩阵运算造成很大困扰。。。
语文太差举个例子:
比如说对于矩阵
matrix(rep(1,6),ncol=1)
#> [,1]
#> [1,] 1
#> [2,] 1
#> [3,] 1
#> [4,] 1
#> [5,] 1
#> [6,] 1
这个矩阵就能对 一个数做矩阵运算:
matrix(rep(1,6),ncol=1) %*% 1
#> [,1]
#> [1,] 1
#> [2,] 1
#> [3,] 1
#> [4,] 1
#> [5,] 1
#> [6,] 1
<sup>Created on 2019-11-05 by the reprex package (v0.2.1)</sup>
但是如果对第一个矩阵取了下标运算:
matrix(rep(1,6),ncol=1)[1:3,]
#> [1] 1 1 1
此时再做同样的矩阵运算:
matrix(rep(1,6),ncol=1)[1:3,]%*% 1
#> Error in matrix(rep(1, 6), ncol = 1)[1:3,] %*% 1: non-conformable arguments
<sup>Created on 2019-11-05 by the reprex package (v0.2.1)</sup>
就不对了。
强制给每步运算都加一个as.matrix应该也不是不可以,但是想问问有没有更优美的解决方式。。。