r - 如何在单位正方形上多次进行 100 个点的 Voronoi 镶嵌
问题描述
简而言之,我将进行 100 个点的 Voronoi 细分,并创建 1000 次不同的 100 个点集,并对每组点进行细分。
我创建了点,
x=matrix(runif(100*1000),nrow=100,ncol=1000)
y=matrix(runif(100*1000),nrow=100,ncol=1000)
spatstat
使用包执行 Voronoi 细分的基本代码是
dir = ppp(x=x, y=y, window = square(c(0,1)))
tess = dirichlet(dir)
plot(tess, main = "")
points(dir, pch=19, cex = 0.5)
但是,我需要对 1000 个样本进行 Voronoi 细分并尝试创建一个循环。我想选择 x 和 y 的每一列,最终得到 1000 个“目录”。然后为 1000 'dir' 进行镶嵌 'tess'。我还需要使用函数计算 voronoi 细胞的面积area=tile.areas(tess)
我创建的循环是
for (i in 1000) {
dir[i] = ppp(x=x[,i], y=y[,i], window = square(c(0,1)))
}
但我得到的只是错误和警告。你知道怎么做吗?
解决方案
您需要将输出存储在一个对象中,在这种情况下,让我们将它放在一个名为dirList
. 此外,您必须指定要迭代的序列。for (i in 100)
什么都不做,它必须是for (i in 1:100)
library(deldir)
library(spatstat)
x <- matrix(runif(100 * 1000), nrow = 100, ncol = 1000)
y <- matrix(runif(100 * 1000), nrow = 100, ncol = 1000)
dirList <- list()
for (i in 1:1000){
dirList[[i]] <- ppp(x = x[ , i], y = y[ , i], window = square(c(0, 1)))
}
然后进行绘图,您可以使用[[]]
tess = dirichlet(dirList[[1]])
plot(tess, main = "")
对于问题的第二部分,将您的对象tess
用于一组点:
归功于@D杰克的一般过程
library(deldir)
library(spatstat)
library(maptools)
library(rgeos)
x <- matrix(runif(100 * 1000), nrow = 100, ncol = 1000)
y <- matrix(runif(100 * 1000), nrow = 100, ncol = 1000)
x <- x[ , 1]
y <- y[ , 1]
dir = ppp(x=x, y=y, window = square(c(0,1)))
tess = dirichlet(dir)
t1 <- as(tess, "SpatialPolygons")
t2 <- unionSpatialPolygons(t1, ID=rep(1,length(t1)))
vor_border <- which(gRelate(t1,t2, byid=TRUE) %in% c("2FF11F212"))
par(mfcol=c(1,3))
plot(t1)
plot(t1[vor_border,], col="red")
# you could also invert the mask to get just the inner polygons
plot(t1[-vor_border,], col="lightblue")
推荐阅读
- python - 如何在 Tkinter GUI 中将相同的方法应用于包含不同小部件的两个不同框架
- python - 'float' 对象不能被解释为整数 - 从 python2.X 到 python3.8
- http-status-code-404 - rust/mdBook:如果地址是子文件夹,则显示不带 CSS 的 404 页面
- mongodb - 如何强制 mongo db $or 查询首先检查第一个提供的值,如果没有找到则只回退到第二个?
- laravel - 车辆与价格之间的雄辩关系
- node.js - AWS Lambda:同步调用流
- java - Kafka Connect:Rest API Source Connector 连接被拒绝
- php - 注释路线并在内部扩展现有方法
- python - 在 Mac 上安装 GDAL 后,jupyter-lab 中没有名为“gdal”的模块
- javascript - AWS Amplify JS 如何选择 Cognito 用户组?