2-D and 3-D Grids - MATLAB Meshgrid
2-D and 3-D Grids - MATLAB Meshgrid
meshgrid
2-D and 3-D grids
Syntax
[X,Y] = meshgrid(x,y)
[X,Y] = meshgrid(x)
[X,Y,Z] = meshgrid(x,y,z)
[X,Y,Z] = meshgrid(x)
Description
[X,Y] = meshgrid(x,y) returns 2-D grid coordinates based on the coordinates contained in vectors x example
and y. X is a matrix where each row is a copy of x, and Y is a matrix where each column is a copy of y. The
grid represented by the coordinates X and Y has length(y) rows and length(x) columns.
example
[X,Y] = meshgrid(x) is the same as [X,Y] = meshgrid(x,x), returning square grid coordinates with
grid size length(x)-by-length(x).
[X,Y,Z] = meshgrid(x,y,z) returns 3-D grid coordinates defined by the vectors x, y, and z. The grid example
represented by X, Y, and Z has size length(y)-by-length(x)-by-length(z).
example
[X,Y,Z] = meshgrid(x) is the same as [X,Y,Z] = meshgrid(x,x,x), returning 3-D grid coordinates
with grid size length(x)-by-length(x)-by-length(x).
2-D Grid
Create 2-D grid coordinates with x-coordinates defined by the vector x and y-
coordinates defined by the vector y. Try This Example
Copy Command
x = 1:3; Get
y = 1:5;
[X,Y] = meshgrid(x,y)
X = 5×3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
https://www.mathworks.com/help/matlab/ref/meshgrid.html 1/8
4/2/24, 11:50 PM 2-D and 3-D grids - MATLAB meshgrid
Y = 5×3
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
ans = 5×3
2 5 10
5 8 13
10 13 18
17 20 25
26 29 34
Plot Surface
Create a 2-D grid with uniformly spaced x-coordinates and y-coordinates in the
interval [-2,2]. Try This Example
Copy Command
x = -2:0.25:2; Get
y = x;
[X,Y] = meshgrid(x);
2 2
F = X.*exp(-X.^2-Y.^2); Get
surf(X,Y,F)
https://www.mathworks.com/help/matlab/ref/meshgrid.html 2/8
4/2/24, 11:50 PM 2-D and 3-D grids - MATLAB meshgrid
Starting in R2016b, it is not always necessary to create the grid before operating over it. For example, computing
2 2
the expression xe− x − y implicitly expands the vectors x and y. For more information on implicit expansion, see
Array vs. Matrix Operations.
surf(x,y,x.*exp(-x.^2-(y').^2)) Get
https://www.mathworks.com/help/matlab/ref/meshgrid.html 3/8
4/2/24, 11:50 PM 2-D and 3-D grids - MATLAB meshgrid
3-D Grid
Create 3-D grid coordinates from x-, y-, and z-coordinates defined in the interval
[0,6], and evaluate the expression x
2
+ y
2
+ z
2
. Try This Example
Copy Command
x = 0:2:6; Get
y = 0:1:6;
z = 0:3:6;
[X,Y,Z] = meshgrid(x,y,z);
F = X.^2 + Y.^2 + Z.^2;
Determine the size of the grid. The three coordinate vectors have different lengths, forming a rectangular box of
grid points.
gridsize = 1×3
7 4 3
Use the single-input syntax to generate a uniformly spaced 3-D grid based on the coordinates defined in x. The new
grid forms a cube of grid points.
gridsize = 1×3
4 4 4
x — x-coordinates of points
vector
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
y — y-coordinates of points
vector
z — z-coordinates of points
vector
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
x-coordinates over a grid, returned as a 2-D (two inputs) or 3-D array (three inputs).
y-coordinates over a grid, returned as a 2-D (two inputs) or 3-D array (three inputs).
You can convert between these grid formats using pagetranspose (as of R2020b) or permute to swap the first two
dimensions of the grid arrays. For example, create a 3-D grid with meshgrid.
[X,Y,Z] = meshgrid(1:4,1:3,1:2);
Now transpose the first two dimensions of each grid array to convert the grid to ndgrid format, and compare the
results against the outputs from ndgrid.
https://www.mathworks.com/help/matlab/ref/meshgrid.html 5/8
4/2/24, 11:50 PM 2-D and 3-D grids - MATLAB meshgrid
Xt = pagetranspose(X);
Yt = pagetranspose(Y);
Zt = pagetranspose(Z);
[Xn,Yn,Zn] = ndgrid(1:4,1:3,1:2);
isequal(Xt,Xn) & isequal(Yt,Yn) & isequal(Zt,Zn)
ans =
logical
1
Using pagetranspose is equivalent to permuting the first two dimensions while leaving other dimensions the
same. You can also perform this operation using permute(X,[2 1 3:ndims(X)]).
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel
Computing Toolbox™ ThreadPool.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing
Toolbox™.
Version History
Introduced before R2006a
See Also
griddedInterpolant | mesh | ndgrid | combinations | surf
Topics
Interpolating Gridded Data
https://www.mathworks.com/help/matlab/ref/meshgrid.html 6/8
4/2/24, 11:50 PM 2-D and 3-D grids - MATLAB meshgrid
Learn more
https://www.mathworks.com/help/matlab/ref/meshgrid.html 7/8
4/2/24, 11:50 PM 2-D and 3-D grids - MATLAB meshgrid
https://www.mathworks.com/help/matlab/ref/meshgrid.html 8/8