1
比方說,我們有這樣的矩陣:的R - n個最小值的矩陣的上三角獲取dimnames
set.seed(1)
m <- matrix(abs(rnorm(16)), ncol=4)
rownames(m) <- colnames(m) <- c('a', 'b', 'c', 'd')
m
# a b c d
# a 0.6264538 0.3295078 0.5757814 0.62124058
# b 0.1836433 0.8204684 0.3053884 2.21469989
# c 0.8356286 0.4874291 1.5117812 1.12493092
# d 1.5952808 0.7383247 0.3898432 0.04493361
是否有可能,以高效的方式,來獲得的維度名稱,說,上三角形的3個最小值?
我有以下方法:
#find the 3rd smallest value in the upper triag:
val <- m[upper.tri(m)][order(m[upper.tri(m)])[3] ]
#get the indices of values smaller than val:
ind_smallest <- arrayInd(which(upper.tri(m) & m <=val), dim(m))
cbind(colnames(m)[ind_smallest[, 1]], rownames(m)[ind_smallest[, 2]])
# [,1] [,2]
# [1,] "a" "b"
# [2,] "a" "c"
# [3,] "b" "c"
是否有更簡單的方法,這既是內存和時間效率?