使用NumPy meshgrid查找n维空间中立方体或超立方体的坐标

rslzwgfq  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(103)

所以我似乎不能越过终点线上的一个问题,我有。
我试图定义一个n维坐标空间,然后将该空间划分为k**d组d维单元,其中d是维度,k是用于划分轴的bin的数量。
如果我使用以下方法为单元格生成一组边界:

points = []
n_bins = 3
for dim in range(3):
    boundary = np.linspace(0, 2, n_bins+1)
    points.append(boundary)

对于一个三维问题,这在for循环中重复三次,因为实际上每个维度都有不同的范围要合并。
然后按如下方式创建网格:

points = np.array(points)
grid = np.stack(np.meshgrid(*points)).T

这似乎生成了必要的点组合,可用于找到每个立方体的坐标,如下图所示。然而,我完全被困在如何为这个例子提取9个立方体的坐标上。
由于我被困在这个简单的问题上,我甚至不能开始处理如何在高维空间中提取超立方体的坐标。有人能在这方面提供一些帮助吗?
抱歉,我没有提供足够的细节。

[[[[0. 0. 0.]
   [0. 1. 0.]
   [0. 2. 0.]
   [0. 3. 0.]]

  [[1. 0. 0.]
   [1. 1. 0.]
   [1. 2. 0.]
   [1. 3. 0.]]

  [[2. 0. 0.]
   [2. 1. 0.]
   [2. 2. 0.]
   [2. 3. 0.]]

  [[3. 0. 0.]
   [3. 1. 0.]
   [3. 2. 0.]
   [3. 3. 0.]]]

 [[[0. 0. 1.]
   [0. 1. 1.]
   [0. 2. 1.]
   [0. 3. 1.]]

  [[1. 0. 1.]
   [1. 1. 1.]
   [1. 2. 1.]
   [1. 3. 1.]]

  [[2. 0. 1.]
   [2. 1. 1.]
   [2. 2. 1.]
   [2. 3. 1.]]

  [[3. 0. 1.]
   [3. 1. 1.]
   [3. 2. 1.]
   [3. 3. 1.]]]

 [[[0. 0. 2.]
   [0. 1. 2.]
   [0. 2. 2.]
   [0. 3. 2.]]

  [[1. 0. 2.]
   [1. 1. 2.]
   [1. 2. 2.]
   [1. 3. 2.]]

  [[2. 0. 2.]
   [2. 1. 2.]
   [2. 2. 2.]
   [2. 3. 2.]]

  [[3. 0. 2.]
   [3. 1. 2.]
   [3. 2. 2.]
   [3. 3. 2.]]]

 [[[0. 0. 3.]
   [0. 1. 3.]
   [0. 2. 3.]
   [0. 3. 3.]]

  [[1. 0. 3.]
   [1. 1. 3.]
   [1. 2. 3.]
   [1. 3. 3.]]

  [[2. 0. 3.]
   [2. 1. 3.]
   [2. 2. 3.]
   [2. 3. 3.]]

  [[3. 0. 3.]
   [3. 1. 3.]
   [3. 2. 3.]
   [3. 3. 3.]]]]
brqmpdu1

brqmpdu11#

好吧,第三次魅力。
我在每个轴上生成一个分裂点的列表。对于3个部分的8个立方体,产生[0,3,6,8]。然后,我对该列表进行偏移压缩,以生成开始/结束点:[0,3],[3,6],[6,8]]
这三个元素的所有组合都产生了立方体集。然后打印坐标只是另一个product来合并组合起点和终点的问题:

import itertools
import numpy as np
cube = 8
n_bins = 3

pts = list(range(0,cube,n_bins)) + [cube]
print(pts)
pairs = list(zip(pts[:-1],pts[1:]))
print(pairs)

for p1 in itertools.product( *([pairs]*3) ):
    print("Subcube:")
    for p2 in itertools.product(*p1):
        print(p2)

输出量:

[0, 3, 6, 8]
[(0, 3), (3, 6), (6, 8)]
Subcube:
(0, 0, 0)
(0, 0, 3)
(0, 3, 0)
(0, 3, 3)
(3, 0, 0)
(3, 0, 3)
(3, 3, 0)
(3, 3, 3)
Subcube:
(0, 0, 3)
(0, 0, 6)
(0, 3, 3)
(0, 3, 6)
(3, 0, 3)
(3, 0, 6)
(3, 3, 3)
(3, 3, 6)
Subcube:
(0, 0, 6)
(0, 0, 8)
(0, 3, 6)
(0, 3, 8)
(3, 0, 6)
(3, 0, 8)
(3, 3, 6)
(3, 3, 8)
Subcube:
(0, 3, 0)
(0, 3, 3)
(0, 6, 0)
(0, 6, 3)
(3, 3, 0)
(3, 3, 3)
(3, 6, 0)
(3, 6, 3)
Subcube:
(0, 3, 3)
(0, 3, 6)
(0, 6, 3)
(0, 6, 6)
(3, 3, 3)
(3, 3, 6)
(3, 6, 3)
(3, 6, 6)
Subc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