本文介绍了一维矩阵更改为R中的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
> a<-matrix(c(1:9),3,3)
> a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> a[3,]*a[,3] # I expect 1x1 matrix as result of this.
[1] 21 48 81
> class(a)
[1] "matrix"
> class(a[3,])
[1] "integer"
在R中,一维矩阵更改为矢量.我可以避免这种情况吗?我想保留一维矩阵作为矩阵.实际上,我需要向RcppArmadillo抛出多种矩阵,甚至是零维矩阵.将矩阵本身更改为向量是我的问题.
In R, 1-dimensional matrix is changed to a vector. Can I avoid this?I would like to keep 1-D matrix as a matrix. Actually, I need to throw many kind of matrix to RcppArmadillo, even zero-D matrix. Changing matrix to vector by itself is my problem.
推荐答案
这是R 常见问题解答.您需要执行a[3,,drop = FALSE]
.
This is an R FAQ. You need to do a[3,,drop = FALSE]
.
这篇关于一维矩阵更改为R中的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!