How to slice a n dimensional array with a m*(n-i) dimensional matrix?

廉价感情. 提交于 2019-12-06 13:20:03

问题


If i have a n dimensional array it can be sliced by a m * n matrix like this

a <- array(1:27,c(3,3,3))

b <- matrix(rep(1:3,3),3)

# This will return the index a[1,1,1] a[2,2,2] and a[3,3,3]
a[b]

# Output
[1]  1 14 27

Is there any "effective and easy" way to do a similar slice but to keep some dimensions free? That is slice a n dimensional array with a m * (n-i) dimensional array and get a i+1 dimensional array as result.

a <- array(1:27,c(3,3,3))

b <- matrix(rep(1:2,2),2)

# This will return a vector of the index a[1] a[2] a[1] and a[2]
a[b]

# Output
[1] 1 2 1 2


# This will return the indexes of the cartesian product between the vectors,
# that is a array consisting of a[1,,1] a[1,,2] a[2,,1] and a[2,,2]
a[c(1,2),,c(1,2)]

# Output
, , 1

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8

, , 2

     [,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17

The desired result should be if the last command returned an array with a[1,,1] and a[2,,2]. For now I solve this the problem with a for loop and abind but I'm sure there must be a better way.

# Desired functionality
a <- array(1:27,c(3,3,3))
b <- array(c(c(1,2),c(1,2)),c(2,2))
sliceem(a,b,freeDimension=2)

# Desired output (In this case rbind(a[1,,1],a[2,,2]) ) 
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]   11   14   17

回答1:


I think this is the cleanest way -- making a separate function:

slicem <- function(a,idx,drop=FALSE) do.call(`[`,c(list(a),idx,list(drop=drop)))

# usage for OP's example
a      <- array(1:27, c(3,3,3))    
idx    <- list(1:2, TRUE, 1:2)
slicem(a,idx)

which gives

, , 1

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8

, , 2

     [,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17

You have to write TRUE for each dimension that you aren't selecting from.


Following the OP's new expectations...

library(abind)
nistfun <- function(a,list_o_idx,drop=FALSE){
  lens <- lengths(list_o_idx)
  do.call(abind, lapply(seq.int(max(lens)), function(i)
    slicem(a, mapply(`[`, list_o_idx, pmin(lens,i), SIMPLIFY=FALSE), drop=drop)
  ))
}

# usage for OP's new example
nistfun(a, idx)

# , , 1
# 
#      [,1] [,2] [,3]
# [1,]    1    4    7
# 
# , , 2
# 
#      [,1] [,2] [,3]
# [1,]   11   14   17

Now, any non-TRUE indices must have the same length, since they will be matched up.

abind is used here instead of rbind (see an earlier edit on this answer) because it is the only sensible general way to think about slicing up an array. If you really want to drop dimensions, it's quite ambiguous which should be dropped and how, so the vector alone is returned:

nistfun(a, idx, drop=TRUE)
# [1]  1  4  7 11 14 17

If you want to throw this back into an array of some sort, you can do that after the fact:

matrix( nistfun(a, idx), max(lengths(idx)), dim(a)[sapply(idx,isTRUE)]), byrow=TRUE)

#      [,1] [,2] [,3]
# [1,]    1    4    7
# [2,]   11   14   17


来源:https://stackoverflow.com/questions/31925595/how-to-slice-a-n-dimensional-array-with-a-mn-i-dimensional-matrix

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