我现在正在尝试将具有3维坐标(x,y,z)的n个点投影到具有特定大小(例如64 * 64)的xy网格上,当然,在这种情况下,此类n点的坐标受到限制网格。
目标是打印投影到每个网格元素上的点的z坐标。我编写了两个for循环,但是还有什么更好的方法来避免使用for循环更快地运行它?
for i in range(XY_grid.shape[0]):
x = np.where((X_coordinate > i) & (X_coordinate <= i + 1), 1, 0)
for j in range(XY_grid.shape[1]):
y = np.where(( Y_coordinate > j) & (Y_coordinate <= j + 1), 1, 0)
print(x * y * Z_coordinate)
答案 0 :(得分:0)
要打印与X_coordinate
和Y_coordinate
中的特定点相对应的for i in range(XY_grid.shape[0]):
for j in range(XY_grid.shape[1]):
print(Z_coordinate[np.logical_and(X_coordinate==i, Y_coordinate==j)])
的所有条目,您可以执行以下操作:
runBlockingTest
答案 1 :(得分:0)
我认为您想要的是2D直方图:
import numpy as np
# generate some data (x, y, z)
x = np.arange(100)
y = np.random.rand(100)
z = np.arange(100)[::-1] * 1.5
# grid (x, y) onto a defined grid (0-127) in x and y
grid, xe, ye = np.histogram2d(x, y, bins=(np.arange(128), np.arange(128)), weights=None)
grid.sum()
>>> 100.0 # all data is in the grid (was only 100 points)
您可以使用weight
参数添加z
值:
# grid (x, y) onto a defined grid (0-127) in x and y
grid, xe, ye = np.histogram2d(x, y, bins=(np.arange(128), np.arange(128)), weights=z)
grid.sum()
>>> 7425.0
z.sum()
>>> 7425.0 # all z values are in the produced grid
您可以更改bins
的宽度等以使其不均匀,或将它们均匀地隔开以形成规则的网格。
结果grid
是一个二维numpy数组,其中包含落入每个bin中的所有z
信息。您可以轻松地print
或在其上循环以依次获取每个元素。