I got the following issue:
I have a large array "x" of the dimension dim(x)= (46, 13, 30). I'm trying to compute a new matrix "M"(30,598) which contains basically the first element of every "slice" in the first column, the second element in the second and so on... Eventually, I want to work with vectors (columns of M) containing all the elements of each grid cell.
With the following function I would be able to calculate my matrix M. However, it's not exactly a user-friendly solution considering the large array:
M <- cbind(x[1,1,],x[1,2,],x[1,3,],x[1,4,],x[2,1,],x[2,2,], x[2,3,],x[2,4,]......)
That's why I want to use a for-loop. Unfortunately I can't figure out how.
This is what I tried so far (unsuccessfully):
M <- matrix(NA, nrow = 6, ncol = 20)
for (i in 1:120){
M[i] <- cbind(x[,,i])
}
This loop only creates the first vector (first element of every "slice" [1,1,]), than aborts.
Any ideas on how to get this done? Very much appreciate the help!
set.seed(1) ; x <- array(rnorm(2*3*5), dim = c(2, 3, 5))for instance. And a related desired outputdesired_output <- cbind(x[1,1,],x[1,2,],x[1,3,],x[2,1,],x[2,2,], x[2,3,])?t(apply(x, 3, identity))