Find the indices of the 5 closest samples in the distance matrix

Users

I have a dMat distance matrix and you want to find the 5 closest samples for the first. What function can I use in R? I know how to find the closest sample (see 3rd line of code), but I can’t figure out how to get the remaining 4 samples.

The code:

Mat <- replicate(10, rnorm(10)) dMat <- as.matrix(dist(Mat)) which(dMat[,1]==min(dMat[,1])) 

The third line of code finds the index of the closest pattern to the first pattern.

Thanks for any help!

Best, Chega

+8
matrix r distance
source share
2 answers

You can use order for this:

 head(order(dMat[-1,1]),5)+1 [1] 10 3 4 8 6 

Please note that I deleted the first one, since you apparently do not want to include the fact that your control point is at a distance of 0 from yourself.

+6
source share

Alternative use of sort :

 sort(dMat[,1], index.return = TRUE)$ix[1:6] 

It would be nice to add set.seed(.) When using random numbers in the matrix so that we can show that the results are identical. I will skip the results here.

Edit (correct solution): The above solution will only work if the first element is always the smallest! Here is the correct solution, which will always give 5 closest values ​​to the first element of the column:

 > sort(abs(dMat[-1,1] - dMat[1,1]), index.return=TRUE)$ix[1:5] + 1 

Example:

 > dMat <- matrix(c(70,4,2,1,6,80,90,100,3), ncol=1) # James' solution > head(order(dMat[-1,1]),5) + 1 [1] 4 3 9 2 5 # values are 1,2,3,4,6 (wrong) # old sort solution > sort(dMat[,1], index.return = TRUE)$ix[1:6] [1] 4 3 9 2 5 1 # values are 1,2,3,4,6,70 (wrong) # Correct solution > sort(abs(dMat[-1,1] - dMat[1,1]), index.return=TRUE)$ix[1:5] + 1 [1] 6 7 8 5 2 # values are 80,90,100,6,4 (right) 
+5
source share

All Articles