如何为另一个向量中的每个元素获取向量中最接近的元素而没有重复? [英] How to get the closest element in a vector for every element in another vector without duplicates?
问题描述
我得到了这段代码,它创建了两个向量,对于 a
中的每个元素,我想获得 b
中最接近的元素:
I got this code which create two vectors and for each element from a
I want to get the closest element in b
:
a = rnorm(100)
b = rnorm(100)
c = vapply(a, function(x) which.min(abs(b - x)), 1)
table(duplicated(c))
FALSE TRUE
61 39
如您所见,此方法会提示提供大量重复项,这很正常,但我不想有重复项.我想在选择索引后从 b
中删除出现,但我不知道如何在 vapply
下进行.
As you can see this method is prompt to give a lot of duplicates which is normal but I would like to not have duplicates. I thought of deleting occurence from b
once an index has been selected but I don't know how to do it under vapply
.
推荐答案
最接近的匹配是对向量进行排序,然后将它们配对.b
上的以下排列应该允许您这样做.
The closest match you are going to get is by sorting the vectors and then pairing them off. The following permuation on b
should allow you to do that.
p <- order(b)[order(order(a))] # order on b and then back transform the ordering of a
sum(abs(a-b[p]))
[1] 20.76788
显然,允许重复确实会让事情变得更接近:
Clearly, allowing duplicates does make things much closer:
sum(abs(a-b[c]))
[1] 2.45583
这篇关于如何为另一个向量中的每个元素获取向量中最接近的元素而没有重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!