0

I am mostly aware of the np.meshgrid function, that, given two vectors as an input, would create two other vectors that would correspond to the x and y coordinates of a mesh grid spanned by the two vectors (is this even correct?)

However, I can't seem to find a "nice" way to turn these vectors into a real coordinate grid.

Assuming I want to print the positions of each gridpoints on a 2D grid, I would, for example, write the following code

x = np.linspace(1,3,3)
y = np.linspace(1,3,3)
xx, yy = np.meshgrid(x, y)


for ii in range(xx.shape[0]):
    for jj in range(xx.shape[1]):
            print(xx[ii][jj], yy[ii][jj])

To me it feels like this is a rather inefficient approach, and it is hard to generalize to an arbitrary number of dimensions (I'd need to write the loops by hand I assume, or come up with a tricky iterator).

Is there a more elegant way to generate the coordinates of the mesh grid automatically?

Edit: I should probably point our that the answer here seems not to work anymore

0 Answers0