Skip to content
Advertisement

Cartesian product of x and y array points into single array of 2D points

I have two numpy arrays that define the x and y axes of a grid. For example:

JavaScript

I’d like to generate the Cartesian product of these arrays to generate:

JavaScript

In a way that’s not terribly inefficient since I need to do this many times in a loop. I’m assuming that converting them to a Python list and using itertools.product and back to a numpy array is not the most efficient form.

Advertisement

Answer

JavaScript

See Using numpy to build an array of all combinations of two arrays for a general solution for computing the Cartesian product of N arrays.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement