|
・In Japanese
■Explanation of meshgrid function
Generates a grid array. A similar function is mgrid.
■Examples of meshgrid functions
The following must be written in common for each concrete example. To install numpy, click here.
Example
x = [1,2,3,4]
y = [10,20,30]
X,Y = np.meshgrid(x,y)
print(X)
array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
print(Y)
array([[10, 10, 10, 10],
[20, 20, 20, 20],
[30, 30, 30, 30]])
X represents the horizontal axis and Y represents the vertical axis.

The advantage of doing this is that it allows you to perform lattice calculations all at once, as shown below.
print(X+Y)
array([[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34]])
You can also create combinations of axes as follows:
X=X.reshape(12,1) #Rearrange to 12x1
Y=Y.reshape(12,1)
print(np.vstack((X,Y))) #Combine axes horizontally
array([[ 1, 10],
[ 2, 10],
[ 3, 10],
[ 4, 10],
[ 1, 20],
[ 2, 20],
[ 3, 20],
[ 4, 20],
[ 1, 30],
[ 2, 30],
[ 3, 30],
[ 4, 30]])
|
|