Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在寻找一种基于a=(x1,y1,z1) and b=(x2,y2,z2)立方体的两个相对端点 ( ) 创建 3D 网格的方法。每个维度的步长为 1。刚刚发现了一些以 开头的 numpy fct (0,0,0),在我的情况下,尽管起点是可变的和负的(例如(-5,-10,-15))。
a=(x1,y1,z1) and b=(x2,y2,z2)
(0,0,0)
(-5,-10,-15)
使用np.ogrid(或者np.mgrid如果你想要一个密集的网格):
np.ogrid
np.mgrid
>>> x1, y1, z1 = -4, -3, -2 >>> x2, y2, z2 = 4, 3, 2 >>> np.ogrid[x1:x2, y1:y2, z1:z2] [array([[[-4]], [[-3]], [[-2]], [[-1]], [[ 0]], [[ 1]], [[ 2]], [[ 3]]]), array([[[-3], [-2], [-1], [ 0], [ 1], [ 2]]]), array([[[-2, -1, 0, 1]]])]
列表推导 FTW!
xRng = range(a[0],b[0]+1) yRng = range(a[1],b[1]+1) zRng = range(a[2],b[2]+1) g = [(a,b,c) for a in xRng for b in yRng for c in zRng]