Bert Gunter
2008-Jan-07 17:34 UTC
[R] Efficient way to substract all entries in two vectors from eachother
Vectors of what? How is "closest" defined? See e.g. ?dist Bert Gunter Genentch Nonclinical Statistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Johannes Graumann Sent: Monday, January 07, 2008 11:19 AM To: r-help at stat.math.ethz.ch Subject: [R] Efficient way to substract all entries in two vectors from eachother Hi all, I'm to inexperienced to come up with the matrix solution elusively appearing on the horizon for the following problem and would appreciate if you could give me a nudge ... I have two vectors a, and b and need to find the closest match for each value of a in b. How to do that efficiently? Thanks, Joh ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Johannes Graumann
2008-Jan-07 19:18 UTC
[R] Efficient way to substract all entries in two vectors from each other
Hi all, I'm to inexperienced to come up with the matrix solution elusively appearing on the horizon for the following problem and would appreciate if you could give me a nudge ... I have two vectors a, and b and need to find the closest match for each value of a in b. How to do that efficiently? Thanks, Joh
Ido M. Tamir
2008-Jan-07 19:35 UTC
[R] Efficient way to substract all entries in two vectors from each other
Johannes Graumann wrote:> Hi all, > > I'm to inexperienced to come up with the matrix solution elusively > appearing on the horizon for the following problem and would appreciate if > you could give me a nudge ... > I have two vectors a, and b and need to find the closest match for each > value of a in b. > How to do that efficiently?I am not sure if I understood your problem correctly, but maybe this is of help. However its not memory efficient.> a <- c(3,10,50,100) > b <- c(7,8,300) > b[apply(abs(outer(a,b,"-")),1,which.min)][1] 7 8 8 8 In Biobase there is the matchpt function doing this efficiently. best wishes, ido