Arrays 如何创建子数组访问for()中数组的第i维?

Arrays 如何创建子数组访问for()中数组的第i维?,arrays,r,Arrays,R,在for循环中,我在一个数组上运行I,我想在维度I中对该数组进行子索引。如何做到这一点?因此,一个最简单的例子是 (A <- array(1:24, dim = 2:4)) A[2,,] # i=1 A[,1,] # i=2 A[,,3] # i=3 #构建多维数组 A#现在就来点完全不同的东西吧! #构建多维数组 A谢谢你的帮助,瑞克 subindex <- c(2,1,3) # in the ith dimension, we would like to subindex by

在for循环中,我在一个数组上运行
I
,我想在维度
I
中对该数组进行子索引。如何做到这一点?因此,一个最简单的例子是

(A <- array(1:24, dim = 2:4))
A[2,,] # i=1
A[,1,] # i=2
A[,,3] # i=3
#构建多维数组
A
#现在就来点完全不同的东西吧!
#构建多维数组

A谢谢你的帮助,瑞克
subindex <- c(2,1,3) # in the ith dimension, we would like to subindex by subindex[i]
for(i in seq_along(dim(A))) {
    args <- list(1:2, 1:3, 1:4)
    args[i] <- subindex[i]
    print(do.call("[", c(list(A), args)))
}
#Build a multidimensional array
A <- array(1:24, dim = 2:4)
# Select a sub-array 
indexNumber = 2
indexSelection = 1

# Build a parameter list indexing all the elements of A
parameters <- list(A, 1:2, 1:3, 1:4)
# Modify the appropriate list element to a single value
parameters[1 + indexNumber] <- indexSelection   
# select the desired subarray
do.call("[", parameters)
# Now for something completely different!
#Build a multidimensional array
A <- array(1:24, dim = 2:4)
# Select a sub-array 
indexNumber = 2
indexSelection = 1

reduced <- A[slice.index(A, indexNumber) == indexSelection]
dim(reduced) <- dim(A)[-indexNumber]

# Also works on the left-side
A[slice.index(A, 2)==2] <- -1:-8