简体   繁体   English

如何从 R 中的二维矩阵创建 3D 数组?

[英]How to create a 3D array from 2D matrices in R?

I would like to create a 3D array based on a couple of 2D matrices in R, but I have no idea.我想基于 R 中的几个二维矩阵创建一个 3D 数组,但我不知道。 Let's say we have the following three matrices:假设我们有以下三个矩阵:

matrix1 <- matrix(1:1, nrow = 5, ncol = 5)
matrix2 <- matrix(2:2, nrow = 5, ncol = 5)
matrix3 <- matrix(3:3, nrow = 5, ncol = 5)

I would like to know how to create one [1:3, 1:5, 1:5] array, as a combination of the three matrices.我想知道如何创建一个[1:3, 1:5, 1:5]数组,作为三个矩阵的组合。 Thank you!谢谢!

We can concatenate the matrixes together into a vector, use array to construct a 3D array with specified dim我们可以将矩阵连接在一起形成一个向量,使用array构造一个指定dim的3D数组

ar1 <- array(c(matrix1, matrix2, matrix3), c(5, 5, 3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM