I want to create a list of all possible (x,y) values.
For example - for input (x=2,y=3) the output should be:
np.array([
[0,0],
[0,1],
[0,2],
[1,0],
[1,1],
[1,2]])
The order doesn't really matter
I want to create a list of all possible (x,y) values.
For example - for input (x=2,y=3) the output should be:
np.array([
[0,0],
[0,1],
[0,2],
[1,0],
[1,1],
[1,2]])
The order doesn't really matter
Quick and dirty:
>>> np.argwhere(np.ones((2, 3)))
array([[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2]])
Inspired by Divakar's indices_merged_arr
, this should be fast.
def indices_for(m, n):
i, j = np.ogrid[:m, :n]
v = np.empty((m, n, 2), dtype=np.uint32)
v[..., 0] = i
v[..., 1] = j
v.shape = (-1, 2)
return v
>>> indices_for(3, 2)
array([[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2]])
m, n = 10000, 10000
%timeit indices_for(m, n)
607 ms ± 9.12 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit np.argwhere(np.ones((m, n)))
4.69 s ± 225 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)