简体   繁体   中英

Find minimum distances between rows of two matrices

I have two tables, X and Y, (X is big and Y has 9 rows, of course same columns) and I need to find the minimum euclidean distance between each row of X with each row of Y. I do this and it works:

x<-matrix(c(3,6,3,4,8),nrow=5,ncol=7,byrow = TRUE)     
y<-matrix(c(1,4,4,1,9),nrow=5,ncol=7,byrow = TRUE)

unlist(lapply(seq_len(nrow(y)), function(i) min(sqrt(colSums((y[i, ] -t(x))^2))))

Now I need to export which row of Y (1 to 9) is the one for each row, and there is my problem, because I do not know how to face this. Any clue about how to write this? I've been thinking about doing something like:

unlist(lapply(seq_len(nrow(y)), function(i) nrow(min(sqrt(colSums((y[i, ] - t(x))^2)))==T)))

but I cannot make it work.

Thank you!

You can do this easily with my imputation package:

Sys.setenv("PKG_CXXFLAGS"="-std=c++0x") # needed for the lambda functions in Rcpp

# install/load package, create example data
devtools::install_github("alexwhitworth/imputation")
library(imputation)
set.seed(123)
a <- matrix(rnorm(10000), ncol= 10)
b <- matrix(rnorm(100), ncol=10)

# which row of a is closest to each row of b
apply(b, 1, function(i, a) {
  which.min(imputation:::dist_q.matrix(rbind(i, a), ref= 1L, q=2))
}, a= a)
[1] 471 502 555 969 692 757 116 913 556 566

# which row of b is closest to each row of a
apply(a, 1, function(i, b) {
  which.min(imputation:::dist_q.matrix(rbind(i, b), ref= 1L, q=2))
}, b= b)
### result not shown since it's large

Technically, you don't need the arguments ref and q since 1L and 2 are the defaults.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM