Skip to content
Prev 43296 / 398506 Next

distance between two matrices

Sundar,

I'm not sure how much faster (or slower) this might be (perhaps Professor
Ripley will help on this one), but one can "trick" a function from the class
package called knn1 into doing this for you, I think.  From your example:
+    d <- (A[, 1] - B[j, 1])^2 + (A[, 2] - B[j, 2])^2
+    m[j] <- which.min(d)
+ }
[1] 3 2 3

Now using knn1:

 > knn1(A,B,seq(1,nrow(A),1))
[1] 3 2 3
Levels: 1 2 3 4 5 6

Sean

----- Original Message -----
From: "Sundar Dorai-Raj" <sundar.dorai-raj at pdf.com>
To: "R-help" <r-help at stat.math.ethz.ch>
Sent: Tuesday, January 27, 2004 3:00 PM
Subject: [R] distance between two matrices
http://www.R-project.org/posting-guide.html