寻找相邻点集的策略

时间:2017-07-21 18:04:48

标签: r distance

拥有一组点的3D坐标我希望找到比某一距离更近的点的“链”。

为了给你一个直观的例子,这个R代码生成下面的图像,其中小于10个单位的点用红线连接。

set.seed(12345)

num.points <- 150

pts <- data.frame(PosX = runif(num.points, 0, 100),
                  PosY = runif(num.points, 0, 100),
                  PosZ = runif(num.points, 0, 50))

plot(pts$PosX, pts$PosY, pch = 20, cex = 2, xlab = "X", ylab = "Y", bty = "n", las = 1)

d <- as.matrix(dist(pts))

neighbour <- matrix(d<10, ncol = ncol(d))

for(r in 2:nrow(neighbour))
  for(c in 1:(r-1))
  {
    if (neighbour[r,c] == T)
    {
      segments(pts$PosX[r], pts$PosY[r], 
               pts$PosX[c], pts$PosY[c],
               col = "red", lwd = 2)
    }
  }

example plot

我想识别直接或通过其他点连接在一起的点集。

我想找出一些不依赖慢for循环的东西,但我似乎找不到明显的解决方案,所以任何想法都会受到赞赏。

0 个答案:

没有答案