`which()` function for matrix indices

后端 未结 2 718
滥情空心
滥情空心 2020-12-09 07:51

Say I have some matrix, for example:

> m = matrix(rep(c(0, 0, 1), 4), nrow = 4)
> m
     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    0    1    0
[3,]            


        
2条回答
  •  悲哀的现实
    2020-12-09 08:19

    For your first question you need to also pass arr.ind= TRUE to which:

    > which(m == 1, arr.ind = TRUE)
         row col
    [1,]   3   1
    [2,]   2   2
    [3,]   1   3
    [4,]   4   3
    

提交回复
热议问题