Problem
import numpy as np
I have an an array, without any prior information of its contents. For example:
ourarray = \
np.array([[0,1],
[2,3],
[4,5]])
I want to get the pairs of numbers which can be used for indexing ourarray
. Ie I want to get:
array([[0, 0, 1, 1, 2, 2],
[0, 1, 0, 1, 0, 1]])
(0,0
, 0,1
, 1,0
, etc., all the possible indices of ourarray
are in this array.)
Similar but different posts
how to find indices of a 2d numpy array occuring in another 2d array: here they search for one array within another one, not returning indices of the entire array.
Find indices of rows of numpy 2d array in another 2D array: they are dealing with two arrays to start with, the objective isn't to create a second array based on the first one containing its indices
Attempt 1 (Successful but inefficient)
I can get this array by:
np.array(np.where(np.ones(ourarray.shape)))
Which gives the desired result but it requires creting np.ones(ourarray.shape)
, which seems like not an efficient way of doing it.
Attempt 2 (Failed)
I also tried:
np.array(np.where(ourarray))
which does not work because there is no indices returned for the 0
entry of ourarray
.
Question
Attempt 1 works, but I am looking for a more efficient way. How can I do this more efficiently?