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.

0

1 Answer 1

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]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.