Cartesian Product Of x And y Array Points Into single Array Of 2D Points

In this post, we will learn the Cartesian product of x and y array points into a single array of 2D points which we can perform using the meshgrid() function of the numpy module and the example of the same we have given below where we can easily find how we can use and implement it as per our choice.

Cartesian

 

 

Cartesian Product

As Cartesian product simply means aXb where each elements os a is multiplied with each element of b or when can say the output of the result of it will be always equal to the a*b and all the combinations are available in the output and an example of same we have given below where we can easily find out how we can do so and execute as per our choice.

import numpy as np

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

xx, yy = np.meshgrid( x, y)
points = np.column_stack(( xx.ravel(), yy.ravel()))

print( points)

Here we have taken two lists of sizes three named as x and y and they get it to and saved all of them in the form of xx and yy which simply printed as a result at the end where we want to do the thing.

The code given above will result in the following output which we have given below

[[1 4]
 [2 4]
 [3 4]
 [1 5]
 [2 5]
 [3 5]
 [1 6]
 [2 6]
 [3 6]]

Here we simply multiplied the number of elements in x by the number of elements in y which in this case are three and three respectively so we simply multiply them as per the rule says so the output will have the result as 3*3 equals to nine elements which we can clearly see in the output where the result shows nothing but all the possible combinations of each and every element of both the lists.

Here the thing we need to keep in mind is the shape of the resulting array which will always be ‘(len( x)* len( y, 2)’ as even the shape of that resulting output is dependent upon the sizes of the lists we multiplied here.

We need it at a number of places for example when we need to find the joins in SQL we have to get this so that we can get all the values stored in both the tables could we get and then we can provide the filter and get the desired data and get t done.

 

 

To learn more about the Cartesian product of x and y array points into a single array of 2D points visit:  by Stack overflow

To learn more about solutions to different problems and tutorials for the concepts we need to know to work on programming along with different ways to solve any generally asked problems: How To Pass-Variables From A Java & Python Client To A Linux/Ubuntu Server Which Is Running C?.

 

Leave a Comment

%d bloggers like this: