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:
JavaScript
x
7
1
N = 10
2
array = np.array([[0,0]])
3
for i in range(N):
4
for j in range(N):
5
array = np.append(array, [[i,j]], axis=0)
6
print(array)
7
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:
JavaScript
1
9
1
data=np.indices((512,512)).swapaxes(0,2).swapaxes(0,1)
2
data.shape
3
# output: (512, 512, 2)
4
5
data[5,0]
6
# output: array([5, 0])
7
data[5,25]
8
#output: array([5, 25])
9