shup
shup

Reputation: 120

Generating a multidimensional grid in python

I have a discretized 8-dimensional bounded space for which I want to get a grid over all possible combinations in a shape of (N,8). It should look like:

import numpy as np
myGrid = np.array([[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,2],...])

The bounds of all 8 dimensions are not equal.

Upvotes: 0

Views: 212

Answers (1)

Paul Panzer
Paul Panzer

Reputation: 53029

You can use indices and moveaxis:

np.moveaxis(np.indices(<your shape>), 0, -1).reshape(-1, 8)

This will be zero-based, so add 1 to get exactly your desired output.

Upvotes: 1

Related Questions