如何获得均匀间隔阵列的位置坐标(n×2)?

How to get positional coordinates (n×2) for a uniformly spaced array?

我正在尝试使用来自 PsychoPy 的 ElementArrayStim 制作一个线元素数组(23×23 网格)。

对于线元素位置的xys参数,我试图让线元素以统一的方式(23×23网格)定位。

我试图通过执行以下操作来获取元素的位置:

nx, ny = (23, 23)
xaxis = np.linspace(-220, 220, nx)
yaxis = np.linspace(-220, 220, ny)

yx = np.meshgrid(xaxis, yaxis)

我从中收到的输出似乎是 2 个单独的数组(我假设 x 轴坐标和 y 轴坐标),但它们似乎是按每一行列出的。

但是,PsychoPy 仅接受 `xys 参数的 n×2 输入 - 我不确定如何更改输出的形状,使其成为 n×2 的形式。

此外,如果我使用的方法是 incorrect/inefficient,那么在 n×2 形状中实现 xys 位置元素的最佳方法是什么?

您是否需要遍历两个轴,边走边将每个元素附加到数组?

fred=np.array([[],[]])
for column in xaxis:
    for row in yaxis:
        fred = np.append(fred, [[column], [row]], 1)

fred.shape
(2, 529)

你非常接近,只需要从 xaxisyaxis 创建一个 3D 坐标数组,然后根据需要重塑该 3D 数组以获得 529 行 × 2 列的 2D 数组:

In [21]: xy = np.dstack(np.meshgrid(xaxis, yaxis)).reshape(-1, 2)

In [22]: xy
Out[22]: 
array([[-220, -220],
       [-200, -220],
       [-180, -220],
       ..., 
       [ 180,  220],
       [ 200,  220],
       [ 220,  220]])

In [23]: xy.shape
Out[23]: (529L, 2L)

或者,您也可以通过以下方法获得相同的结果:

xy = np.mgrid[-220:240:20, -220:240:20].T.reshape(-1, 2)