0

I would like to convert 2 numpy arrays such as these ones:

a = [[1, 2, 3]]
b = [[100, 200, 300]]

to an array like below.

[[1, 100], [1, 200], [1, 300], [2, 100], [2, 200], [3, 300], [3, 100], [3, 200], [3, 300]]   

Is this possible in NumPy?

Thanks in advance.

(edited to clarify the point of this question.) I'm trying to find a numpy way of solution.

feedMe
  • 3,431
  • 2
  • 36
  • 61
D. Jin
  • 3
  • 2

1 Answers1

1

This is a job for meshgrid and stack:

a = np.array([ [1, 2, 3] ])
b = np.array([ [100, 200, 300] ])

print(np.stack(np.meshgrid(a, b)).T.reshape(-1,2))

The first creates a tuple of coordinate on the grid, the second stacks them. Then you just need to transpose and flatten.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62