I have 2 multidimensional arrays - a 4D array and a 3D array - and some code to to find the maximum of the 4D array along a dimension, and make an index for selecting from the 3D array based on this. At the moment it's quite slow and I'd like to speed things up.
Reprex:
library(microbenchmark)
# Make some arrays to test with
array4d <- array( runif(5*500*50*5 ,-1,0),
dim = c(5, 500, 50, 5) )
array3d <- array( runif(5*500*5, 0, 1),
dim = c(5, 500, 5))
# The code of interest
microbenchmark( {
max_idx <- apply(array4d, c(1,2,3), which.max )
selections <- list()
for( i in 1:dim(array4d)[3] ){
selections[[i]] <- apply(array3d, c(1,2), which.max) == max_idx[ , , i]
}
})
Any tips appreciated!
(A side issue is I'm considering replacing which.max
by nnet::which.is.max
to have random breaking of ties)
Edit: A faster solution thanks to @GKi, but I'm still hoping for some speedups:
max_idx <- apply(array4d, c(1,2,3), which.max)
max_idx2 <- apply(array3d, c(1,2), which.max)
selections <- lapply(seq_len(dim(array4d)[3]), function(i) max_idx2 == max_idx[ , , i])