Given the following array in R:
x <- 1:24
dim(x) <- c(2, 4, 3)
Which gives:
> x
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
, , 2
[,1] [,2] [,3] [,4]
[1,] 9 11 13 15
[2,] 10 12 14 16
, , 3
[,1] [,2] [,3] [,4]
[1,] 17 19 21 23
[2,] 18 20 22 24
How to apply the sample() function to permute the columns across the matrices ? For instance in column 1, the value 1, 2, 9, 10 and 17, 18 would be permuted across the 3 matrices while staying in column 1.
One sampling could be:
> x
, , 1
[,1] [,2] [,3] [,4]
[1,] 10 11 5 23
[2,] 2 19 22 8
, , 2
[,1] [,2] [,3] [,4]
[1,] 1 3 6 24
[2,] 17 12 14 16
, , 3
[,1] [,2] [,3] [,4]
[1,] 9 4 21 7
[2,] 18 20 13 15