Get the row and column name of the minimum element of a matrix

前端 未结 1 499
余生分开走
余生分开走 2020-11-29 02:33

I need to get the row and column name of the smallest element of a matrix

> mat = matrix(data=runif(12), nrow = 4, ncol=4)
> rownames(mat) = colnames(m         


        
相关标签:
1条回答
  • 2020-11-29 02:46
    > inds = which(mat == min(mat), arr.ind=TRUE)
    > inds
      row col
    a   1   2
    > rnames = rownames(mat)[inds[,1]]
    > cnames = colnames(mat)[inds[,2]]
    

    This will give you the row/column names for each entry that equals the minimum value; if you just want the first one, you could only check inds[1,1] and inds[1,2].

    0 讨论(0)
提交回复
热议问题