[R] distance between two matrices

Sean Davis sdavis2 at mail.nih.gov
Wed Jan 28 12:16:51 CET 2004


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:

> set.seed(1)
> A <- matrix(runif(12), 6, 2) # 6 x 2
> B <- matrix(runif(6), 3, 2)  # 3 x 2
> m <- vector("numeric", nrow(B))
> for(j in 1:nrow(B)) {
+    d <- (A[, 1] - B[j, 1])^2 + (A[, 2] - B[j, 2])^2
+    m[j] <- which.min(d)
+ }
> m
[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


> Hi all,
>    Say I have a matrix A with dimension m x 2 and matrix B with
> dimension n x 2. I would like to find the row in A that is closest to
> the each row in B. Here's an example (using a loop):
>
> set.seed(1)
> A <- matrix(runif(12), 6, 2) # 6 x 2
> B <- matrix(runif(6), 3, 2)  # 3 x 2
> m <- vector("numeric", nrow(B))
> for(j in 1:nrow(B)) {
>    d <- (A[, 1] - B[j, 1])^2 + (A[, 2] - B[j, 2])^2
>    m[j] <- which.min(d)
> }
>
> All I need is m[]. I would like to accomplish this without using the
> loop if possible, since for my real data n > 140K and m > 1K. I hope
> this makes sense.
>
> Thanks,
> Sundar
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list