获取矩阵最小元素的行名和列名 [英] Get the row and column name of the minimum element of a matrix
本文介绍了获取矩阵最小元素的行名和列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获取矩阵最小元素的行和列名称
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(mat) = letters[1:4]
>
> mat
a b c d
a 0.3167865 0.6958895 0.4233572 0.3167865
b 0.1042599 0.1552235 0.8461520 0.1552235
c 0.6286461 0.9749868 0.2390978 0.6286461
d 0.5923721 0.7823673 0.8427426 0.5923721
> min = min(mat)
> min
> 0.1042599
在此示例中,我想获得"a"和"b"
In this example I'd like to get "a" and "b"
推荐答案
> inds = which(mat == min(mat), arr.ind=TRUE)
> inds
row col
a 1 2
> rnames = rownames(mat)[inds[,1]]
> cnames = colnames(mat)[inds[,2]]
这将为您提供等于最小值的每个条目的行/列名称;如果只想要第一个,则只能检查inds [1,1]和inds [1,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].
这篇关于获取矩阵最小元素的行名和列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文