问题描述
在R中处理矩阵时,可以并排放置它们,也可以分别使用cbind和rbind将它们彼此堆叠在一起.在其他维度上堆叠矩阵或数组的等效函数是什么?
When working with matrices in R, one can put them side-by-side or stack them top of each other using cbind and rbind, respectively. What is the equivalent function for stacking matrices or arrays in other dimensions?
例如,以下代码创建一对2x2矩阵,每个矩阵具有4个元素:
For example, the following creates a pair of 2x2 matrices, each having 4 elements:
x = cbind(1:2,3:4)
y = cbind(5:6,7:8)
将它们组合成具有8个元素的2x2x2数组的代码是什么?
What is the code to combine them into a 2x2x2 array with 8 elements?
推荐答案
请参见abind
程序包.如果您希望它们在三维尺寸上绑定,请执行以下操作:
See the abind
package. If you want them to bind on a 3rd dimension, do this:
library(abind)
abind(x, y, along = 3)
请参见?abind
此外,abind
带来了更多的便利,但是对于简单的绑定,您可以根据默认顺序直接操作值:
Also, abind
gives a lot more convenience, but for simple binding you can just manipulate the values directly, based on the default ordering:
array(c(x, y), dim = c(2, 2, 2))
这篇关于R与rbind和cbind的多维等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!