0

I have two numpy arrays as the coordinates of rows and columns of a matrix:

r = np.array([1, 2, 3])
c = np.array([7, 8, 9])

Is there a simple way to create a new array of the coordinates of all cells of the matrix, like:

m = np.SOME_FUNCTION(r, c)
# m = array([[1, 7], [1, 8], [1, 9], [2, 7], [2, 8], [2, 9], [3, 7], [3, 8], [3, 9]])

Thanks.

Cuteufo
  • 501
  • 6
  • 15

1 Answers1

1

You can use numpy.meshgrid:

m = np.c_[np.meshgrid(r, c)].T.reshape(-1, 2)

Or numpy.repeat and numpy.tile:

m = np.c_[np.repeat(r, len(c)), np.tile(c, len(r))]

Output:

array([[1, 7],
       [1, 8],
       [1, 9],
       [2, 7],
       [2, 8],
       [2, 9],
       [3, 7],
       [3, 8],
       [3, 9]])
mozway
  • 194,879
  • 13
  • 39
  • 75