根据 0.5 的容差阈值 (±) 比较两个数字向量 [英] Compare two vectors of numbers based on threshold of tolerance (±) of 0.5

查看:26
本文介绍了根据 0.5 的容差阈值 (±) 比较两个数字向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个向量 gh.我想比较这两个向量中的数字,并找出它们之间是否有任何共同元素.但是公共元素不必完全相同,可以在 (-0.5, +0.5) 的范围内.因此,g±0.5h±0.5 进行比较.

g <- c(0.5, 5956.3, 38, 22.666, 590.3, 21.992, 9.3)h <- c(0.7, 99.2, 39, 30, 21.68, 9.4, 22.333, 0.001, 0.000222, 9.999)

例如,在上面的两个向量中,g 中的 0.5h 中的 0.7 匹配,因为它们之间的距离在 ±0.5 附近.9.49.3 也匹配.此外,22.66622.333 也匹配,因为它们的差异也在 (-0.5, +0.5) 范围内.

需要注意的是,gEVERY element 应该与 hEVERY element 进行比较.

R 中是否有一个函数可以做到这一点?

不幸的是,

all.equal 函数只将一个向量中的每个元素与另一个向量中具有相同索引的元素进行比较,因此期望向量的长度相等.我想要做的是我想将向量 g 的每个元素与向量 h 的每个元素进行比较.

解决方案

您可以使用 outer 将 all by all 减去并将这些差异(它们的绝对值)设置为小于或等于到 0.5,即

m1 <- which(abs(outer(g, h, `-`)) <= 0.5, arr.ind = TRUE)

给出,

<块引用>

 row col #where row = g and col = h[1,] 1 1[2,] 6 5[3,] 7 6[4,] 4 7[5,] 6 7[6,] 1 8[7,] 1 9

您可以尝试获得所需的输出(您没有指定想要的输出方式).这是一种方法,

cbind(g = g[m1[,1]], h = h[m1[,2]])# g h# [1,] 0.500 0.700000# [2,] 21.992 21.680000# [3,] 9.300 9.400000# [4,] 22.666 22.333000# [5,] 21.992 22.333000# [6,] 0.500 0.001000# [7,] 0.500 0.000222

I have two vectors g and h. I want to compare the numbers in these two vectors and find out whether there are any common elements between them. But the common elements do not have to be exactly the same and can be within a range of (-0.5, +0.5). Therefore, g±0.5 is being compared with h±0.5.

g <- c(0.5, 5956.3, 38, 22.666, 590.3, 21.992, 9.3)
h <- c(0.7, 99.2, 39, 30, 21.68, 9.4, 22.333, 0.001, 0.000222, 9.999)

As an example, in the two vectors above, 0.5 from g and 0.7 from h match because they are in the vicinity of ±0.5 from each other. 9.4 and 9.3 also match. And furthermore, 22.666 and 22.333 also match, because their difference is also in the range (-0.5, +0.5).

It is important to note that EVERY element of g should be compared to EVERY element of h.

Is there a function to do this in R?

all.equal function unfortunately only compares each element from one vector to the element with the same index from another vector and thus expects equal length from the vectors. What I want to do is that I want to compare each element of vector g with each element of vector h.

解决方案

You can use outer to subtract all by all and condition those differences (the absolute value of them) to be less than or equal to 0.5, i.e.

m1 <- which(abs(outer(g, h, `-`)) <= 0.5, arr.ind = TRUE)

which gives,

     row col   #where row = g and col = h
[1,]   1   1
[2,]   6   5
[3,]   7   6
[4,]   4   7
[5,]   6   7
[6,]   1   8
[7,]   1   9

You can play around to get desired output (you did not specify how you want it). Here is one way,

cbind(g = g[m1[,1]], h = h[m1[,2]])

#            g        h
#    [1,]  0.500  0.700000
#    [2,] 21.992 21.680000
#    [3,]  9.300  9.400000
#    [4,] 22.666 22.333000
#    [5,] 21.992 22.333000
#    [6,]  0.500  0.001000
#    [7,]  0.500  0.000222

这篇关于根据 0.5 的容差阈值 (±) 比较两个数字向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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