0
votes

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!

1
Welcome to SO. What do you want the output to look like? Please edit your question and provide a few rows of what you think the correct output should be. Also, I'm confused about A and B versus x and y. - C8H10N4O2
also, see ?which.min - C8H10N4O2
I finally made it work by using which.min, and now the solution is fast and easy. Thank you so much for your help - Francisco Calvo Pérez
you can refer this one where I provided different methods w/ high performance. - Patric

1 Answers

1
votes

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.