R,从高维数组中选择子矩阵序列 [英] R, select sequence of sub matrix from a high dimensional array

查看:27
本文介绍了R,从高维数组中选择子矩阵序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A 是一个尺寸为 100*100*100*100 的 4 维数组.我想从 A 的最后两个维度中选择 10000 个子矩阵.B 和 C 是长度为 10000 的向量.它们是选择标准.B 指定 A 的行号,C 指定列号.

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))

使用 for 循环:

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

user  system elapsed 
10.20    0.14   10.34

使用映射:

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.

推荐答案

诀窍是将 A 重新变暗为 3D 数组,以便您可以使用我们所谓的正常"索引.

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

一些示例数据:

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 的方法:

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 

建议的解决方案:

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

这篇关于R,从高维数组中选择子矩阵序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆