I am trying to get an array containing the indices of every item in a N x M array data
as coordinates in the format (N*M) x 2 such that the first column of the array contains row indices and the second column contains column indices for data
. The length of the resulting array would be N*M so that every item in data
as a row. Alternatively, a list of tuples with coordinate information would also suffice.
In:
0 1 2
-------------
0 | a | b | c |
1 | e | f | g |
2 | h | i | j |
-------------
Out:
array([0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2],
... )
I've only been able to find questions from people making the opposite conversion, unfortunately. My ultimate goal is to feed this array into scipy.spatial.distance.cdist() to get the distance of every point from every other point. I am working with [(X,Y) -> Z] raster data, so I can't use cdist on the cell values as it would typically be used. I'm new to numpy, so this has been a bit of head scratcher. I've looked into np.meshgrid, np.column_stack, and np.unravel_index, but I haven't been able to make them work with the output from np.indices.