gpt4 book ai didi

r - 如何获得两个向量的交点?

转载 作者:行者123 更新时间:2023-12-04 05:12:37 25 4
gpt4 key购买 nike

a = c(1,5,2,6,3,6,3,5,7)
b= c(5,3,5,7,2,6,9,3,6)
plot(a,type = "l")
lines(b)

是否有获取两个矢量的所有交点的函数?

最佳答案

使用R的空间设施:

library(sp)     ## Provides basic spatial classes/methods, incl. SpatialLines
library(rgeos) ## Supports topological operations, including intersection

## Read in data and wrap them up as SpatialLines objects
a = c(1,5,2,6,3,6,3,5,7)
b = c(5,3,5,7,2,6,9,3,6)
SL1 <- SpatialLines(list(Lines(Line(cbind(seq_along(a),a)), "A")))
SL2 <- SpatialLines(list(Lines(Line(cbind(seq_along(b),b)), "B")))

## Find intersections
coords <- coordinates(gIntersection(SL1, SL2))

## Check that it worked
plot(a,type = "l")
lines(b)
points(coords, col="red", pch=16)

关于r - 如何获得两个向量的交点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21647982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com