两个连续轮廓的交点

Intersection point of two sequential profiles

我有两个标识配置文件向量(值介于 0 和 1 之间)和固定长度(由基础序列确定)。

我想计算两个配置文件的交叉点,即代表性配置文件(具有更高标识)发生变化的点。

我目前的解决方案是基于连续数的符号计算和乘积计算。你能想象出更优雅的方式吗?

a <- c(1,1,1,0.8,0.8,0.8)
b <- c(0.8,0.8,0.8,1,1,1)

z <- sign(a - b)

res <- sapply(2:length(z),function(i){ z[i-1]*z[i] })
idx <- which(res == "-1")

plot(x=1:length(a),y=a,type="b")
points(x=1:length(b),y=b,type="b")
abline(v=idx,col="red")

可能,您可以使用 signdiff

得到 idx
which(diff(sign(a - b)) != 0)
#[1] 3

所以如果你有 ab 作为

a <- c(1,1,1,0.8,0.8,0.8,1,1)
b <- c(0.8,0.8,0.8,1,1,1,0.8,0.8)

这会return

idx <- which(diff(sign(a - b)) != 0)
idx
#[1] 3 6

情节看起来像:

plot(x= 1:length(a),y=a,type="b")
points(x=1:length(b),y=b,type="b")
abline(v=idx,col="red")