生成X,Y,Z与meshgrid坐标并保存到.csv

发布于 2025-01-28 03:06:09 字数 788 浏览 1 评论 0原文

我正在尝试创建一个均匀间隔的3D PointCloud/网格,并将其导出到CSV。

我设法找到了解决此问题的解决方案(请参阅链接和代码bellow),但可悲的是没有解释,我正在挠头,试图理解循环3的原因。

资料来源:我如何导出x,y,y,y,y,y,y,y数组?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2025-02-04 03:06:09

您有3个循环,因为您使用的是3个轴。

Nunpy Meshgrid创建ND坐标数组。例如,简单的2D数组在每个方向上有2个点:

x = ['x0', 'x1']
y = ['y0', 'y1']

x_mesh, y_mesh = np.meshgrid(x, y)

print('x')
print(x_mesh)
print('y')
print(y_mesh)
print('---')

将打印出:

x
[['x0' 'x1']
 ['x0' 'x1']]
y
[['y0' 'y0']
 ['y1' 'y1']]
---

因此,要获得单个坐标(例如'x0,y0'),您需要将它们加入其中。由于是2D,您只需要2个循环。

coords = []

for a, b in  zip(x_mesh, y_mesh):
    for a1, b1 in zip(a, b):
        coords.append((a1, b1))

print('coords')
print(coords)
print('---')

会打印出来:

coords
[('x0', 'y0'), ('x1', 'y0'), ('x0', 'y1'), ('x1', 'y1')]
---

您有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:

x = ['x0', 'x1']
y = ['y0', 'y1']

x_mesh, y_mesh = np.meshgrid(x, y)

print('x')
print(x_mesh)
print('y')
print(y_mesh)
print('---')

Would print out:

x
[['x0' 'x1']
 ['x0' 'x1']]
y
[['y0' 'y0']
 ['y1' 'y1']]
---

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.

coords = []

for a, b in  zip(x_mesh, y_mesh):
    for a1, b1 in zip(a, b):
        coords.append((a1, b1))

print('coords')
print(coords)
print('---')

Would print out:

coords
[('x0', 'y0'), ('x1', 'y0'), ('x0', 'y1'), ('x1', 'y1')]
---

You have 3 dimensions, so meshgrid has an extra level of nested arrays. Hence the 3 for loops.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文