gpt4 book ai didi

python - 如何在 numpy 中制作带孔的网格?

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:50 25 4
gpt4 key购买 nike

我了解如何制作像这样的简单网格:

low1 = -1; high1 = 1;
n_p = 20
range1 = np.linspace(low1, high1,n_p/2, endpoint=False)
X = np.dstack(np.meshgrid(range1, range1)).reshape(-1, 2)

enter image description here

但是制作如下图所示的网格的最佳方法是什么?

enter image description here

现在我正在构建 8 个矩形并将它们堆叠起来。更好的方法是什么?

最佳答案

可以先创建外矩形,再用滤镜取出内矩形:

x = np.linspace(-2, 2, 20, endpoint=False)
X = np.dstack(np.meshgrid(x, x)).reshape(-1, 2) # outer rectangle

X[(np.abs(X + 0.1) > 1).any(1)].shape # take out the inner rectangle
# (300, 2) 300 = 20 * 20 (outer) - 10 * 10 (inner)

关于python - 如何在 numpy 中制作带孔的网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50244910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com