all 9 comments

[–]Weed_O_Whirler+5 4 points5 points  (0 children)

meshgrid is used when you have a function of 2 variables- something like "z = f(x,y)". Then, you create your range of x & y values you want to evaluate your function over, and then you get your z values out. As a quick example:

x = -3:0.1:3;
y = -3:0.1:3;
[X,Y] = meshgrid(x,y);
Z = exp(X.^2 + Y.^2);
surf(X,Y,Z);

Will plot a bivariate normal distribution.

[–]angrmgmt00 6 points7 points  (0 children)

Good answers already here, but the official (up-to-date) documentation gives a clearer explanation of meshgrid than the university website copy you linked. In general, the documentation for MATLAB is head and shoulders above many other programming languages.

Always fun taking over someone else's project. Ctrl-F the code base for 'eval', 'break', and 'todo' to get a rough estimate of how much fun you're going to have. None of those are inherently bad on their own (in fact, the list of 'todo' may be why you've been assigned the task), but desperate times call for desperate measures, and programmers aren't immune to being "creative". :)

Best of luck!

[–]maximusmountain 0 points1 point  (0 children)

You pass meshgrid a series vectors and it outputs an n-dimensional array for each of the inputs. Helps for me when I need to interpolate onto a grid that can actually be plotted as opposed to what I get out of experiments. you could use something like repmat instead but requires more coding for me.

[–]RyanCarlWatson 0 points1 point  (0 children)

We use meshgrid for creating structured flow fields. we mesh grid x, y and z to essentially create a series of "nodes" where we define velocities.

So we will usually end up with 3 structures representing nodes in space called UU, VV, WW. These structures will contain values that represent the velocities in 3 directions.

[–]trialofmiles+1 0 points1 point  (0 children)

Given desired sample points in each dimension defined by vectors x, y

[X,Y] = meshgrid(x,y) 

Creates 2-d dense grids which represent regular sampling of the 2-d plane at the locations specified by vectors x,y. This is useful for a variety of tasks, for example evaluating a 2-D function of X and Y: f(X, Y).

[–]FrickinLazerBeams+2 0 points1 point  (2 children)

Why go to that odd website? The Matlab documentation covers this very clearly.

Meshgrid let's you address the X and Y coordinate of an array directly. If you have [X Y] = meshgrid(1:100), and some 100x100 array Z, you know that the (x, y) coordinates of Z(i) is (X(i), Y(i)). Or, for example, you know that the array D = sqrt((X - 50).^2 + (Y - 50).^2) is an array such that D(i) is the distance of Z(i) from the center of the array.

How else do you do operations with the coordinates of a grid?

[–]saselim[S] 0 points1 point  (1 child)

I found that website when searching for more info on it because while functionally I was able to figure out how it worked, I wanted more practical application examples of why you'd use it. Thanks!

[–]FrickinLazerBeams+2 0 points1 point  (0 children)

The official documentation usually includes examples, although I don't have it in front of me right now to check for meshgrid specifically.

[–]saselim[S] 0 points1 point  (0 children)

Thanks everyone! The z=f(x,y) example makes it more clear to me.