Home > Software design >  How to create a multi-dimensional grid in python
How to create a multi-dimensional grid in python

Time:08-13

I have seen similar questions but none that need the format of the output array of shape (numpoints, dim)

Here is a simple example of what I have for dim=2

import numpy as np

bounds = [0.5, 0.5]
n = [10,10]
dim = 2
x = np.linspace(-bounds[0], bounds[0], n[0])
y = np.linspace(-bounds[1], bounds[1], n[1])
X, Y = np.meshgrid(x, y)

s = X.shape
data = np.zeros((n[0]*n[1],dim)) 

# convert mesh into point vector for which the model can be evaluated
c = 0
for i in range(s[0]):
    for j in range(s[1]):
        data[c,0] = X[i,j]
        data[c,1] = Y[i,j]
        c = c 1;
plt.scatter(data[:,0], data[:,1])

enter image description here

Is there a faster/better way of doing this so that the data are arranged in this way? I want a general method that could work for any dim.

Edit: Suggested enter image description here

  • Related