Thursday 31 December 2020

How to perform an ND coordinate sweep using numpy meshgrid

I want to create a grid of sampling points in 4 dimensions. I want the points to span from 0 to 10 with equal spacing in each direction. I tried a few iterations of np.meshgrid calls, but I am sure I am doing something wrong. What is the proper way to create these points?

import numpy as np
XL, XU = (0, 10) # lower/upper bounds
MD = 4 # 4 dimensions
x = np.linspace(XL, XU, 20)
np.meshgrid(*[x for ____ in range(MD)])[0].reshape(MD, x.size ** MD // MD).T

array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       ...,
       [10., 10., 10., 10.],
       [10., 10., 10., 10.],
       [10., 10., 10., 10.]])

My current best solution has repeated elements, so I'm sure I must have done something wrong. What has gone wrong here? How can I create the desired grid of equally spaced points?



from How to perform an ND coordinate sweep using numpy meshgrid

No comments:

Post a Comment