如何在python中扩展规则网格上的坐标点

如何在python中扩展规则网格上的坐标点,python,arrays,numpy,Python,Arrays,Numpy,我在规则的x和y网格中有一些坐标点。我想在y网格中扩展我的点: coordinates=np.array([[1., 19., 4.],[2., 19., 4.5],[1., 20., 6.],[2., 20., 6.5],[3., 20., 5.]]) 坐标有两个y网格(唯一的y值):19和20。第一个y网格有两个点,第二个网格有三个点。这些主要点在上传的图中显示为黑点,我想在每个网格的末尾再添加两个新点。然后,第一个网格将有四个点,第二个网格将有五个点。 对于第一个y网格(y=19.),新

我在规则的
x
y
网格中有一些坐标点。我想在
y
网格中扩展我的点:

coordinates=np.array([[1., 19., 4.],[2., 19., 4.5],[1., 20., 6.],[2., 20., 6.5],[3., 20., 5.]])
坐标
有两个
y
网格(唯一的
y
值):
19
20
。第一个
y
网格有两个点,第二个网格有三个点。这些主要点在上传的图中显示为黑点,我想在每个网格的末尾再添加两个新点。然后,第一个网格将有四个点,第二个网格将有五个点。 对于第一个
y
网格(
y=19.
),新的两点具有相同的
y
值,并且它们的
x
值也随着
x
网格(此处为
1.
的差异而增加)。因此,对于第一个新点,我有以下
x
y
值:

3., 19.
4., 19.
对于第二个新点,我有以下
x
y
值:

3., 19.
4., 19.
要查找新点的
z
值,我想根据前两个点的
z
值计算它。对于第一个
y
网格(
y=19.
)中的第一个新点,
x
值为
3
,我在该网格中有两个小于
x
值的点(网格的第一个点:x=1。网格的第二个点:
x=2.
)。网格的第二个点更接近新生成的点,我使用以下等式计算新点的
z
值:

z of closest point + (z of closest point - z of one point before the closest)/2
因此,在我的例子中,网格中壁橱点的
z
4.5
,壁橱前一点的
4
。第三点的z值为:

4.5 + (4.5-4)/2 = 4.75
如果在新生成的点之前只有一个点,我只需将该点的z值复制到新点。 我应该在此网格中创建另一个点,其
x
y
值为
4,19.
。为了找到它的z值,我再次执行相同的规则,并使用第三个点(最近生成)的
z
值和第二个点之前的一个点(第二个点)将其作为:

4.75 + (4.75-4.5)/2 = 4.875
这两个添加的点在我的图中显示为绿色正方形。 对于第二个网格,我也这样做。它已经有三个点了,我再加上两个新点(在我的图中显示为绿色的星星)。新点的
x
y
如下所示:

4., 20. #for the first generated and 5., 20. # for the second generated
对于z值,也应遵循与之前相同的规则:

5.0 + (5.0-6.5)/2. = 4.25 # z value of the first new point coming from third and second points
4.25 + (4.25-5.0)/2. = 3.875 # z value of the second new point coming from fourth point (recently generated) and third points
最后,我希望将这个新的四点添加到我的
坐标
数组中,如下所示:

all_points=np.array([[1., 19., 4.],[2., 19., 4.5],[1., 20., 6.],[2., 20., 6.5],[3., 20., 5.],\
                     [3., 19., 4.75], [4., 19., 4.875], [4., 20., 4.25], [5., 20., 3.875]])
我尝试了以下代码,但首先它不能给我新点的准确
z
值,而且效率也不高:

nums, counts=np.unique(coordinates[:,1],return_counts=True) # gives the y grids
new_y=nums
cum=np.cumsum(counts)-1
new_x=[]
for i in cum:
    new_x.append (coordinates[i,0])
new_x=np.array(new_x)
new_x_y=[]
for m, n in zip (new_x, new_y):
    new_x_y.append([m,n])
    new_x_y.append([m+1,n])
new_x_y=np.array(new_x_y)
z_difference=[]
for i in cum:
    z_difference.append((coordinates[i,-1]-coordinates[i-1,-1])/2)# to find the difference of last two points of each grid
new_z=[]
for i in range (len(cum)-1):
    new_z.append (coordinates[cum[i],-1]+z_difference)#to find the new z of each extrapolated point
new_z=np.array(new_z)
all_z=np.repeat (new_z, repeats=new_z.shape[1], axis=1)
new_points=np.hstack ([new_x_y, all_z.T])
all_points=np.vstack([coordinates, new_points]
在此之前,我非常感谢您的帮助和贡献