将矩阵转换为一维数组
我有一个矩阵(32X48)。
如何将矩阵转换为一维数组?
I have a matrix (32X48).
How can I convert the matrix into a single dimensional array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个矩阵(32X48)。
如何将矩阵转换为一维数组?
I have a matrix (32X48).
How can I convert the matrix into a single dimensional array?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
要么用“scan”读取它,要么只在矩阵上执行 as.vector() 。如果您想要按行或按列转置矩阵,您可能需要先转置矩阵。
Either read it in with 'scan', or just do as.vector() on the matrix. You might want to transpose the matrix first if you want it by rows or columns.
尝试
c()
try
c()
如果我们谈论的是 data.frame,那么你应该问自己这些变量是同一类型的吗?如果是这种情况,您可以使用 rapply 或 unlist,因为 data.frames 是列表,在其灵魂深处......
If we're talking about data.frame, then you should ask yourself are the variables of the same type? If that's the case, you can use rapply, or unlist, since data.frames are lists, deep down in their souls...
array(A)
或array(t(A))
将为您提供一个一维数组。array(A)
orarray(t(A))
will give you a 1-d array.您可以使用
as.vector()
。根据我的小基准,它看起来是最快的方法,如下所示:第一个解决方案使用
as.vector()
,第二个解决方案使用矩阵作为连续数组存储在内存中的事实length(m)
给出矩阵m
中的元素数量。第三个从x
实例化一个array
,第四个使用连接函数c()
。我也尝试过来自gdata
的unmatrix
,但是太慢了,这里就不提了。以下是我获得的一些数值结果:
展平矩阵是机器学习中的常见操作,其中矩阵可以表示要学习的参数,但使用通用库中的优化算法,该算法需要参数向量。因此,将矩阵(或多个矩阵)变换为这样的向量是很常见的。标准 R 函数
optim()
就是这种情况。you can use
as.vector()
. It looks like it is the fastest method according to my little benchmark, as follows:The first solution uses
as.vector()
, the second uses the fact that a matrix is stored as a contiguous array in memory andlength(m)
gives the number of elements in a matrixm
. The third instantiates anarray
fromx
, and the fourth uses the concatenate functionc()
. I also triedunmatrix
fromgdata
, but it's too slow to be mentioned here.Here are some of the numerical results I obtained:
Flattening a matrix is a common operation in Machine Learning, where a matrix can represent the parameters to learn but one uses an optimization algorithm from a generic library which expects a vector of parameters. So it is common to transform the matrix (or matrices) into such a vector. It's the case with the standard R function
optim()
.来自
?matrix
:“矩阵是二维‘数组’的特例。”您可以简单地更改矩阵/数组的维度。From
?matrix
: "A matrix is the special case of a two-dimensional 'array'." You can simply change the dimensions of the matrix/array.可能已经太晚了,无论如何,这是我将矩阵转换为向量的方法:
希望会有所帮助
It might be so late, anyway here is my way in converting Matrix to vector:
hope that will help
简单快速,因为一维数组本质上是一个向量
Simple and fast since a 1d array is essentially a vector
对于那些不仅想要生成数组,还想要生成具有相应行和列名称的数组的人,我建议使用 Melt 函数,如 这个答案。
然后您可以根据需要组合行和列的名称,并使用 spread/pivot_wider 使列名称成为矩阵的行+列名称和 1 行(即向量)的组合。
For anyone looking to produce not just the array, but the array with the corresponding Row and Column names I recommend the melt function as in this answer.
And then you can combine the names of the row and column as you like and use spread/pivot_wider to have the column names be a combination of the row+column names of the matrix and 1 row which is your vector.
您可以使用 Joshua 的解决方案,但我认为您需要 Elts_int <- as.matrix(tmp_int)
或者 for 循环:
z 是一维向量。
You can use Joshua's solution but I think you need
Elts_int <- as.matrix(tmp_int)
Or for loops:
z is a 1d vector.
如果您有一个包含多列的 data.frame (df) 并且您想要矢量化,您可以执行
as.matrix(df, ncol=1)
If you instead had a data.frame (df) that had multiple columns and you want to vectorize you can do
as.matrix(df, ncol=1)