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

Viewed 382

I would like to create a 3D array based on a couple of 2D matrices in R, but I have no idea. 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. Thank you!

1 Answers

We can concatenate the matrixes together into a vector, use array to construct a 3D array with specified dim

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