迭代一个三维数组

iterating a three-dimensional array

他们如何遍历数组的第三维? 检查所有元素?例如:

v =  sample(1:5,24,replace = TRUE)
dim(v) = c(3,2,4)
print(v)

, , 1

     [,1] [,2]
[1,]    3    3
[2,]    4    5
[3,]    3    1

, , 2

     [,1] [,2]
[1,]    5    4
[2,]    4    3
[3,]    2    5

, , 3

     [,1] [,2]
[1,]    5    2
[2,]    5    5
[3,]    1    4

, , 4

     [,1] [,2]
[1,]    5    4
[2,]    1    3
[3,]    3    4

我想创建一个遍历 4 个矩阵的循环

像下面那样尝试v[,,k]

for (k in 1:tail(dim(v), 1)) {
  print(v[, , k])
}