生成X,Y,Z与meshgrid坐标并保存到.csv
我正在尝试创建一个均匀间隔的3D PointCloud/网格,并将其导出到CSV。
我设法找到了解决此问题的解决方案(请参阅链接和代码bellow),但可悲的是没有解释,我正在挠头,试图理解循环3的原因。
import numpy as np
import csv
sp=(30)
x=np.arange(313000, 313120, sp)
y=np.arange(6220000,6220120, sp)
z=np.repeat(15,4)
x_mesh, y_mesh, z_mesh=np.meshgrid(x,y,z)
coords = []
for a, b, c in zip(x_mesh, y_mesh, z_mesh):
for a1, b1, c1 in zip(a, b, c):
for a2, b2, c2 in zip(a1, b1, c1):
coords.append((a2, b2, c2,))
print(coords)
with open('coords.csv','w') as f:
out = csv.writer(f, delimiter=',')
out.writerows(zip(*coords))
f.close()
I'm trying to create an evenly spaced 3D pointcloud/grid and export it to csv.
I managed to find a solution to this problem (see link and code bellow), but sadly there is no explanation and I'm scratching my head, trying to understand why the 3 FOR loops are for.
Source: How do I export x,y,z coordinates from meshgrid arrays?
import numpy as np
import csv
sp=(30)
x=np.arange(313000, 313120, sp)
y=np.arange(6220000,6220120, sp)
z=np.repeat(15,4)
x_mesh, y_mesh, z_mesh=np.meshgrid(x,y,z)
coords = []
for a, b, c in zip(x_mesh, y_mesh, z_mesh):
for a1, b1, c1 in zip(a, b, c):
for a2, b2, c2 in zip(a1, b1, c1):
coords.append((a2, b2, c2,))
print(coords)
with open('coords.csv','w') as f:
out = csv.writer(f, delimiter=',')
out.writerows(zip(*coords))
f.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有3个循环,因为您使用的是3个轴。
Nunpy Meshgrid创建ND坐标数组。例如,简单的2D数组在每个方向上有2个点:
将打印出:
因此,要获得单个坐标(例如'x0,y0'),您需要将它们加入其中。由于是2D,您只需要2个循环。
会打印出来:
您有3个维度,因此Meshgrid具有额外的嵌套数组。因此3循环。
You have 3 for loops because you are using 3 axes.
nunpy meshgrid creates N-D coordinate arrays. For example, simple 2D array with 2 points in each direction:
Would print out:
So, to get the individual coordinates (like 'x0,y0') you need to join these together. As it's 2D you only need 2 for loops.
Would print out:
You have 3 dimensions, so meshgrid has an extra level of nested arrays. Hence the 3 for loops.