如何为另一个向量中的每个元素获取向量中最接近的元素而没有重复? [英] How to get the closest element in a vector for every element in another vector without duplicates?

查看:40
本文介绍了如何为另一个向量中的每个元素获取向量中最接近的元素而没有重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这段代码,它创建了两个向量,对于 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆