3

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.

dotto
  • 45
  • 5

3 Answers3

0

I think you can do:

[*zip(*np.where(np.ones_like(a)))]

Output:

[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

You could use list comprehension.

M = 3
N = 4

[(i, j) for i in range(N) for j in range(M)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2), (3, 0), (3, 1), (3, 2)]
Luke Tonin
  • 51
  • 4
  • This does work, but I am hoping to avoid list comprehension for the sake of speed (and learning numpy better). Thank you though! – dotto Apr 07 '20 at 20:56
0

You can use unravel_index and the shape of the data, it works with individual ints or an array of ints. In your case, you can get it all in one line with:

np.array(np.unravel_index(np.arange(data.size),data.shape)).T
rwalroth
  • 320
  • 2
  • 10
  • My question was to get this as a numpy array ideally, so I think this is the most correct. Thank you! – dotto Apr 07 '20 at 20:59