我有一个对称矩阵mat
:
A B C
A 1 . .
B . 1 .
C . . 1
我想计算它的两个最高元素。现在因为它是一个对称矩阵,我想像upper.tri
这样使用:
mat.tri<-upper.tri(mat) # convert to upper tri
mat.ord<-order(mat.tri,na.last=TRUE,decreasing=TRUE)[1:2] # order by largest
a.ind<-which(mat%in%mat.tri[mat.ord]) # get absolute indices
r.ind<-arrayInd(a.ind,dim(mat)) # get relative indices
# get row/colnames using these indices
所以上面是这样一种迂回的做事方式,即使这样,输出也有“重复”的行,因为它们只是被转置了..
有人有更直观的方法吗?
谢谢。