I want to define an array which contains all combinations of values
[0, 0], [0, 1], [0, 2], …, [0,N],
[1, 0], [1, 1], [1, 2], …, [1, N],
…
[N, 0], [N, 1], [N,2], …, [N,N].
Obviously, one could do something like this:
N = 10 array = np.array([[0,0]]) for i in range(N): for j in range(N): array = np.append(array, [[i,j]], axis=0) print(array)
However, I find this “ugly”. Is there a clean way to generate such an array?
Advertisement
Answer
You can use np.indices
, if you prefer:
data=np.indices((512,512)).swapaxes(0,2).swapaxes(0,1) data.shape # output: (512, 512, 2) data[5,0] # output: array([5, 0]) data[5,25] #output: array([5, 25])