I came across the following problem, currently I do not have an explanation for.
a <- array(seq(24), dim = c(4, 3, 2))
# The axis to be fixed and read completely
axis = c(3)
d <- dim(a)
ds <- lapply(d, seq)
# initialize the list for later slicing
ds[-axis] <- 1
# The axis to be iterated along
axis_iter <- seq_along(d)[-axis]
# Number of iterations
n_iter <- prod(d[-axis])
order <- rev(seq_along(d[-axis]))
# Only for demonstration purposes
tmp <- rep(NA, n_iter)
for (iter in seq(n_iter)) {
tmp[iter] <- iter
# This shows that the values within ds are updated
print(unlist(ds))
# slice array and calculate the minimum value
# Note: That's also only for demonstration purposes
print(min(do.call(`[`, c(list(a), ds, list(drop = TRUE)))))
for (i in order) {
ds[axis_iter][[i]] <- ds[axis_iter][[i]] + 1
if (ds[axis_iter][[i]] <= d[i])
break
else
ds[axis_iter][[i]] <- 1
}
}
The code itself works pretty fine. But I do not know why the updates of the vector tmp remain after the for-loop and for the list ds not. After running the code the values of the list object are the same as before:
> ds
[[1]]
[1] 1
[[2]]
[1] 1
[[3]]
[1] 1 2
Thanks in advance for your help!