R: How to extract sets of multidimensional array values according to another matrix

前端 未结 2 459
终归单人心
终归单人心 2021-01-26 09:11

Suppose I have a 4 dimensional n x n x n x n array A. A is a distance matrix, such that A[i,j,l,k] is the distance from location i,j to location pair l,k. Suppose I have an nxn

2条回答
  •  余生分开走
    2021-01-26 10:15

    I guess you have changed the request from the one stated in the first paragraph. See if this answers the second version.

     A [ cbind( which(T == 0, arr.ind=TRUE), which(T == 1, arr.ind=TRUE) )]
    #[1] 1 1
    

    (At first I thought you might need abind but cbind works fine.)

提交回复
热议问题