Efficiently `apply` on array and preserve structure

天涯浪子 提交于 2019-12-07 07:47:56

问题


I have an array of matrices.

dims <- c(10000,5,5)
mat_array <- array(rnorm(prod(dims)), dims)

I would like to perform a matrix-based operation (e.g. inversion via the solve function) on each matrix, but preserve the full structure of the array.

So far, I have come up with 3 options:

Option 1: A loop, which does exactly what I want, but is clunky and inefficient.

mat_inv <- array(NA, dims)
for(i in 1:dims[1]) mat_inv[i,,] <- solve(mat_array[i,,])

Option 2: The apply function, which is faster and cleaner, BUT squishes each matrix down to a vector.

mat_inv <- apply(mat_array, 1, solve)
dim(mat_inv)
[1]    25 10000

I know I can set the output dimensions to match those of the input, but I'm wary of doing this and messing up the indexing, especially if I had to apply over non-adjacent dimensions (e.g. if I wanted to invert across dimension 2).

Option 3: The aaply function from the plyr package, which does exactly what I want, but is MUCH slower (4-5x) than the others.

mat_inv <- plyr::aaply(mat_array, 1, solve)

Are there any options that combine the speed of base::apply with the versatility of plyr::aaply?

来源:https://stackoverflow.com/questions/37754213/efficiently-apply-on-array-and-preserve-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!