Finding list of positions in multidimensional structure (array)

前端 未结 1 789
天涯浪人
天涯浪人 2020-12-20 18:45

Suppose I have a simple multidimensional structure, like this one:

somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))

I\'m l

相关标签:
1条回答
  • 2020-12-20 19:32

    I think the which function can do that:

    which(somestr==2, arr.ind=TRUE)
    

    (if I understood everything correctly)

    R> set.seed(123)
    R> somestr <- array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
    R> somestr
    , , 1
    
         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    2    2    2    1
    [2,]    2    1    1    2    1
    [3,]    1    2    2    1    1
    [4,]    2    2    1    2    2
    
    ...
    
    , , 6
    
         [,1] [,2] [,3] [,4] [,5]
    [1,]    2    1    1    1    2
    [2,]    1    2    1    2    2
    [3,]    1    2    2    2    2
    [4,]    2    2    1    1    1
    
    R> which(somestr==2, arr.ind=TRUE)
          dim1 dim2 dim3
     [1,]    2    1    1
     [2,]    4    1    1
     [3,]    1    2    1
     [4,]    3    2    1
     [5,]    4    2    1
    ...
    [57,]    2    5    6
    [58,]    3    5    6
    
    0 讨论(0)
提交回复
热议问题