I would like to have a way to index a d-dimensional numpy meshgrid:
- without actually storing the full dense meshgrid in memory
- which supports all of the indexing types supported by the full dense meshgrid
As an example:
x = np.random.randn(Nx)
y = np.random.randn(Ny)
z = np.random.randn(Nz)
x_g, y_g, z_g = np.meshgrid(x, y, z, indexing='ij')
X = np.concatenate([x_g[..., None], y_g[..., None], z_g[..., None]], axis=-1)
Once I have X, I can use all of numpy's indexing methods, for example:
X[10:20,1:9:3,:]
X[(
[0, 1, 3, 5],
[1, 1, 3, 3],
[2, 7, 8, 9]
)]
I have cases in which the product Nx * Ny * Nz is just too large to fit in memory (e.g. Nx = Ny = Nz = 4000), yet I'd like to be able to take out bits of my cube using indexing.
Is there a way I can achieve this without re-coding numpy's indexing logic? The answer in 3d would look like:
def meshgrid_index(x, y, z, index):
# index is the argument normally passed to X.__getitem__
# should support all types of indexing
...
Thanks!