如何在正方形的特定部分生成点
How to generate points in a specific part of a square
假设我有一个大小为10x10
的正方形,然后我将这个正方形分成等份,例如,分成 4 个等份(可以是其他数字,如 2、8、16,...) .
之后,在一个循环中,我想随机选择 4 个部分之一,并在这个正方形中生成一个点。这里我会选择第二个方块。
min.x = 0
max.x=10
min.y=0
max.y=10
xd = xMax-xMin
yd = yMax-yMin
#generating randomly coordinates at the second square
set.seed(1)
xx_1 = 5*runif(1) + 5; yy_1 = 5*runif(1) + 0
#ploting the big square and the point in the second square just to ilustrate
对于这个例子,如果我手动操作,我可以对 4 个方块中的每一个使用以下函数:
xx_1 = 5*runif(1)+0; yy_1 = 5*runif(1)+0
xx_2 = 5*runif(1)+5; yy_2 = 5*runif(1)+0
xx_3 = 5*runif(1)+0; yy_3 = 5*runif(1)+5
xx_4 = 5*runif(1)+5; yy_4 = 5*runif(1)+5
关于如何自动生成特定正方形中的点的任何提示?
您可以使用三个参数编写一个函数:
- 您想去的方格数"to the right"(在您的图片中:0 代表方格 1 和 3,2 代表方格 2 和 4)
您要上升的方格数(0 代表方格 1 和 2,2 代表方格 3 和 4)
正方形的长度
使用这些参数,您应该能够重新修改代码,将 +0/+5 替换为参数 * 正方形的宽度
xx_1 = square_length*runif(1)+right_param * square_length
yy_1 = square_length*runif(1)+upwards_param * square_length
这里有一个小函数可以满足您的要求。你告诉它正方形的大小(即一侧的长度),你想把它切成多少块(这显然应该是一个平方数),以及你想要随机抽样的那块(编号左边到右,从下到上,如您的示例所示)。
square_sample <- function(size = 10, pieces = 4, n = 1)
{
x_min <- ((n - 1) %% sqrt(pieces)) * size/sqrt(pieces)
y_min <- ((n - 1) %/% sqrt(pieces)) * size/sqrt(pieces)
c(x = runif(1, x_min, x_min + size/sqrt(pieces)),
y = runif(1, y_min, y_min + size/sqrt(pieces)))
}
在你的例子上测试一下:我们应该得到一个 x 值在 5 到 10 之间,y 值在 0 到 5 之间的点:
square_sample(size = 10, pieces = 4, n = 2)
#> x y
#> 5.968655 3.254514
或者挑一个150*150的正方形中间的正方形切成9块。这里我们期望 x 和 y 都在 50 到 100 之间:
square_sample(size = 150, pieces = 9, n = 5)
#> x y
#> 78.47472 97.32562
您可以使用复数向量的绝对实部,此代码将生成您想要的任意数量的点。
Npoints = 4 # any multiple of 4 will generate equal number of points in each quarterion
x = Re(1i**(1:Npoints)) %>% abs
y = Re(1i**(0:(Npoints-1))) %>% abs
randoms = lapply(1:(2*Npoints),function(x){
5*runif(1)
})%>% unlist
coor.mat =cbind(x + randoms[1:Npoints],
y + randoms[(Npoints +1) : (2*Npoints)])
现在 coor.mat
应该是一个 2 列矩阵,其中 col1 是 x,col2 是 y,行数是您要生成的点数。
编辑:小更正
x.min = 0
x.max=10
y.min=0
y.max=10
num.random = 100
possible.squares = c(1,2,4,8,16)
squares = sample(possible.squares, 1)
x.length = x.max/squares
y.length = y.max/squares
x.coord = seq(from=x.min, to=x.max, by = x.length)
y.coord = seq(from=y.min, to=y.max, by = y.length)
set.seed(1)
loop {
n = #<which ever square you want>
x.rand = runif (1, min = x.coord[n-1], max = x.coord[n])
y.rand = runif (1, min = y.coord[n-1], max = y.coord[n])
#(x,y) is your coordinate for the random number in the nth square
}
这有帮助吗?
假设我有一个大小为10x10
的正方形,然后我将这个正方形分成等份,例如,分成 4 个等份(可以是其他数字,如 2、8、16,...) .
之后,在一个循环中,我想随机选择 4 个部分之一,并在这个正方形中生成一个点。这里我会选择第二个方块。
min.x = 0
max.x=10
min.y=0
max.y=10
xd = xMax-xMin
yd = yMax-yMin
#generating randomly coordinates at the second square
set.seed(1)
xx_1 = 5*runif(1) + 5; yy_1 = 5*runif(1) + 0
#ploting the big square and the point in the second square just to ilustrate
对于这个例子,如果我手动操作,我可以对 4 个方块中的每一个使用以下函数:
xx_1 = 5*runif(1)+0; yy_1 = 5*runif(1)+0
xx_2 = 5*runif(1)+5; yy_2 = 5*runif(1)+0
xx_3 = 5*runif(1)+0; yy_3 = 5*runif(1)+5
xx_4 = 5*runif(1)+5; yy_4 = 5*runif(1)+5
关于如何自动生成特定正方形中的点的任何提示?
您可以使用三个参数编写一个函数:
- 您想去的方格数"to the right"(在您的图片中:0 代表方格 1 和 3,2 代表方格 2 和 4)
您要上升的方格数(0 代表方格 1 和 2,2 代表方格 3 和 4)
正方形的长度
使用这些参数,您应该能够重新修改代码,将 +0/+5 替换为参数 * 正方形的宽度
xx_1 = square_length*runif(1)+right_param * square_length
yy_1 = square_length*runif(1)+upwards_param * square_length
这里有一个小函数可以满足您的要求。你告诉它正方形的大小(即一侧的长度),你想把它切成多少块(这显然应该是一个平方数),以及你想要随机抽样的那块(编号左边到右,从下到上,如您的示例所示)。
square_sample <- function(size = 10, pieces = 4, n = 1)
{
x_min <- ((n - 1) %% sqrt(pieces)) * size/sqrt(pieces)
y_min <- ((n - 1) %/% sqrt(pieces)) * size/sqrt(pieces)
c(x = runif(1, x_min, x_min + size/sqrt(pieces)),
y = runif(1, y_min, y_min + size/sqrt(pieces)))
}
在你的例子上测试一下:我们应该得到一个 x 值在 5 到 10 之间,y 值在 0 到 5 之间的点:
square_sample(size = 10, pieces = 4, n = 2)
#> x y
#> 5.968655 3.254514
或者挑一个150*150的正方形中间的正方形切成9块。这里我们期望 x 和 y 都在 50 到 100 之间:
square_sample(size = 150, pieces = 9, n = 5)
#> x y
#> 78.47472 97.32562
您可以使用复数向量的绝对实部,此代码将生成您想要的任意数量的点。
Npoints = 4 # any multiple of 4 will generate equal number of points in each quarterion
x = Re(1i**(1:Npoints)) %>% abs
y = Re(1i**(0:(Npoints-1))) %>% abs
randoms = lapply(1:(2*Npoints),function(x){
5*runif(1)
})%>% unlist
coor.mat =cbind(x + randoms[1:Npoints],
y + randoms[(Npoints +1) : (2*Npoints)])
现在 coor.mat
应该是一个 2 列矩阵,其中 col1 是 x,col2 是 y,行数是您要生成的点数。
编辑:小更正
x.min = 0
x.max=10
y.min=0
y.max=10
num.random = 100
possible.squares = c(1,2,4,8,16)
squares = sample(possible.squares, 1)
x.length = x.max/squares
y.length = y.max/squares
x.coord = seq(from=x.min, to=x.max, by = x.length)
y.coord = seq(from=y.min, to=y.max, by = y.length)
set.seed(1)
loop {
n = #<which ever square you want>
x.rand = runif (1, min = x.coord[n-1], max = x.coord[n])
y.rand = runif (1, min = y.coord[n-1], max = y.coord[n])
#(x,y) is your coordinate for the random number in the nth square
}
这有帮助吗?