Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, select sequence of sub matrix from a high dimensional array

A is a 4 dimensional array with dim 100*100*100*100. I want to select 10000 sub matrix from A's last two dimensions. B and C are vectors of length 10000. They are selection criteria. B specifies the row number of A, and C specifies the column number.

A <- array(rnorm(100^4), dim=c(100,100,100,100))
B <- sample( nrow(A) , 10000 , repl = TRUE )
C <- sample( ncol(A) , 10000 , repl = TRUE )
D <- array(0, dim=c(10000,100,100))

With for loop:

system.time(
 for ( i in 1:10000 ){    
     D[i,,] <- A[B[i],C[i],,]
 }) 

user  system elapsed 
10.20    0.14   10.34

with mapply:

sub_array <- function(b,c) return(A[b,c,,])
system.time(D <- mapply(FUN = sub_array, B, C, SIMPLIFY='array'))

user  system elapsed 
9.77    3.75   29.17 

which is even slower. Is there a faster way to do that? Thanks.

like image 409
lionup Avatar asked Nov 02 '22 21:11

lionup


1 Answers

The trick is to re-dim A into a 3D array so you can use what we would call "normal" indexing.

Some sample data:

n <- 60
A <- array(rnorm(n^4), dim=c(n,n,n,n))
B <- sample( nrow(A) , n^2 , repl = TRUE )
C <- sample( ncol(A) , n^2 , repl = TRUE )
D <- array(0, dim=c(n^2,n,n))

OP's method:

system.time({
  D <- array(0, dim=c(n*n, n, n))
  for ( i in 1:(n*n) ) D[i,,] <- A[B[i],C[i],,]
}) 
#    user  system elapsed 
#    2.33    0.08    2.41 

Suggested solution:

system.time({
  d <- dim(A)
  dim(A) <- c(prod(d[1:2]), d[3:4])
  D2 <- A[B + d[1]*(C-1),,]
})
#    user  system elapsed 
#    0.37    0.06    0.44 

And we check that the results are identical:

identical(D, D2)
# [1] TRUE
like image 135
flodel Avatar answered Nov 09 '22 16:11

flodel