RE: [R] distance between two matrices

2004-01-28 Thread Prof Brian Ripley
On Wed, 28 Jan 2004, "Hüsing, Johannes" wrote: > > From: Prof Brian Ripley [mailto:[EMAIL PROTECTED] > > On Wed, 28 Jan 2004, "Hüsing, Johannes" wrote: > > > ?apply for more info. You'll want to know about apply if > > > you want to avoid loops (which is a good approach). > > > > Unfortunately ap

Re: [R] distance between two matrices

2004-01-28 Thread Sean Davis
) [1] 3 2 3 Levels: 1 2 3 4 5 6 Sean - Original Message - From: "Sundar Dorai-Raj" <[EMAIL PROTECTED]> To: "R-help" <[EMAIL PROTECTED]> Sent: Tuesday, January 27, 2004 3:00 PM Subject: [R] distance between two matrices > Hi all, >Say I have a matrix A

RE: [R] distance between two matrices

2004-01-28 Thread Liaw, Andy
You can try and see if knn1() in the `class' package (part of the VR bundle) can handle the job in one shot. If not, just do it in chunks of B. For your example: > id <- 1:nrow(A) > knn1(A, B, id) [1] 3 2 3 Levels: 1 2 3 4 5 6 (I believe it returns factor, but that can easily be converted back

RE: [R] distance between two matrices

2004-01-28 Thread Prof Brian Ripley
On Wed, 28 Jan 2004, "Hüsing, Johannes" wrote: > > 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(r

RE: [R] distance between two matrices

2004-01-28 Thread "Hüsing, Johannes"
> From: Prof Brian Ripley [mailto:[EMAIL PROTECTED] > On Wed, 28 Jan 2004, "Hüsing, Johannes" wrote: > > ?apply for more info. You'll want to know about apply if > > you want to avoid loops (which is a good approach). > > Unfortunately apply() is a wrapper for a for() loop, so will > not help muc

RE: [R] distance between two matrices

2004-01-28 Thread "Jens Oehlschlägel"
Sundar, Have a look at "knn1" from package "class". As I understand what you want is as.integer( knn1(train=A, test=B, cl=1:nrow(A)) ) Best regards Jens Oehlschlägel > 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

RE: [R] distance between two matrices

2004-01-28 Thread "Hüsing, Johannes"
> 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

Re: [R] distance between two matrices

2004-01-28 Thread Sundar Dorai-Raj
Thanks to Prof. Ripley, Jens, Roger, and Sean. knn1 is exactly what I'm looking for. Thanks again, Sundar Prof Brian Ripley wrote: Sounds like knn classification. See function knn1 in package class. knn(A, B, 1:nrow(A)) gives the same answers as your loop code, and is just a carefully tuned

Re: [R] distance between two matrices

2004-01-28 Thread Roger Bivand
On Tue, 27 Jan 2004, Sundar Dorai-Raj wrote: > 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)

[R] distance between two matrices

2004-01-28 Thread Sundar Dorai-Raj
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("num