0

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!

  • 2
    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 Dec 04 '15 at 17:55
  • 1
    also, see `?which.min` – C8H10N4O2 Dec 04 '15 at 18:01
  • 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 Dec 07 '15 at 10:33
  • you can refer [this one](http://stackoverflow.com/questions/27847196/distance-calculation-on-large-vectors-performance/33409695#33409695) where I provided different methods w/ high performance. – Patric Dec 07 '15 at 11:34

1 Answers1

1

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.

alexwhitworth
  • 4,839
  • 5
  • 32
  • 59