Given a matrix, say m, is there any direct method to find top k values of m and then find exactly which column/row do they belong to. I couldn't find any on SO and hence, putting this question. My try on the above has been this:
set.seed(1729)
k=5 #top 5
m = matrix(round(runif(30),digits = 2),nr=10)
idx <- which(matrix(m %in% head(sort(m), k), nr = nrow(m)), arr.ind = TRUE)
print(m)
[,1] [,2] [,3]
[1,] 0.59 0.54 0.57
[2,] 0.44 0.43 0.32
[3,] 0.57 0.08 0.29
[4,] 0.35 0.58 0.24
[5,] 0.86 0.52 0.53
[6,] 0.41 0.78 0.17
[7,] 0.51 0.47 0.26
[8,] 0.15 0.81 0.49
[9,] 0.85 0.64 0.64
[10,] 1.00 0.78 0.95
print(idx)
row col
[1,] 8 1
[2,] 3 2
[3,] 4 3
[4,] 6 3
[5,] 7 3
I am not sure if this is efficient because of the reason that I am sorting the entire values of a matrix rather than picking up those k values. I would like to assume k << length(m). Are there any efficient ways for a large matrix m, and also are there any methods which could help me with duplicates in the scenarios like when one wants to get top k column names
For example: with a matrix mm, I need to identify top 2 columns having least values. Here, for the following case I am expecting columns 1 and 2
mm = matrix(c(6,6,7,8,7,9,8,8,9), 3)
print(mm)
[,1] [,2] [,3]
[1,] 6 8 8
[2,] 6 7 8
[3,] 7 9 9
idx <- which(matrix(mm %in% head(sort(mm), 2), nr = nrow(mm)), arr.ind = TRUE)
print(idx)
row col
[1,] 1 1
[2,] 2 1
But, here I get only one column, i.e.; 1 , In this case, output should be two different columns having least values viz. 1 and 2