Solving MATLAB Meshgrid: A Beginner's Guide

  • Thread starter cookiemnstr510510
  • Start date
  • Tags
    Matlab
In summary: So the final result is an array of all the areas.In summary, meshgrid is a useful function for creating arrays that pair every value in two given arrays, and its implementation is similar to the outer product of two arrays. This allows for easy multiplication of all possible combinations of values, such as length and width, resulting in the desired output. While it may seem convoluted and difficult for beginners, understanding its purpose and how it works can help in utilizing it effectively.
  • #1
cookiemnstr510510
162
14
Homework Statement
The area of a rectangle is length times width. Find the areas of rectangles with lengths of 1,3, and 5cm and with widths of 2,4,6 and 8cm. (you should have 12 answers)
Relevant Equations
meshgrid command in MATLAB
Hello All,
So I know how to solve this problem, but I don't understand it.
I first create a vector for length and width:
L=[1 3 5]
W=[2 4 6 8]
I then use meshgrid to make my L and W vectors have the same dimensions.
[X,Y]=meshgrid(L,W)
Then multiply X and Y together using array multiply (not matrix multiply)
X.*Y

This seems like magic to me.
I know Meshgrid defines X so every row is L, and each of Y's columns is W. I also know that the dimensions of both X and Y have rows equal to length(W) and columns equal to length(L).

I get all that and accept what Meshgrid does internally, I just wonder if there is a logical sort of "proof" of how we get the correct answer from it. It seems a bit convoluted to me as a beginner, and difficult to think about.

Any help is appreciated!
Thanks
 
Physics news on Phys.org
  • #2
cookiemnstr510510 said:
logical sort of "proof"
Don't think the word 'proof' is in order here. The incantation is simply an implementation of the outer product of two arrays.
 
  • Like
Likes cookiemnstr510510
  • #3
The point of meshgrid(X, Y) is to pair every value in X with every value in Y, which is exactly what you want to do.

Suppose you wanted to write out by hand all those combinations of L and W as ordered pairs.
L=[1 3 5]
W=[2 4 6 8]
You might write something like this:
(1, 2) (1, 4) (1, 6) (1,8)
(3, 2) (3, 4) (3, 6) (3, 8)
(5, 2) (5, 4) (5, 6) (5, 8)
There's an array of first values there that looks like this:
1 1 1 1
3 3 3 3
5 5 5 5
and an array of second values that looks like this:
2 4 6 8
2 4 6 8
2 4 6 8

The whole point of meshgrid is to generate those arrays, so that when you match them up element by element you get all the possible combinations in the order you put in.

So when you store those as X and Y, and you multiply them together element by element, you've multiplied every length by every width.
 
  • Like
  • Informative
Likes cookiemnstr510510 and marcusl

1. What is a meshgrid in MATLAB?

A meshgrid in MATLAB is a two-dimensional grid of coordinates that is used to represent a set of points in a specified space. It is commonly used for plotting functions and creating visualizations of data.

2. How do I create a meshgrid in MATLAB?

To create a meshgrid in MATLAB, you can use the "meshgrid" function, which takes two vectors as input representing the x and y coordinates. It will then return two matrices representing the x and y coordinates of each point in the grid.

3. How do I use a meshgrid to plot a function in MATLAB?

To plot a function using a meshgrid in MATLAB, you can first create the meshgrid as described above. Then, you can use the "surf" or "mesh" functions to plot the function on the grid. These functions will create a 3D surface plot or a mesh plot, respectively.

4. Can a meshgrid be used for more than two dimensions in MATLAB?

Yes, a meshgrid can be used for more than two dimensions in MATLAB. The "meshgrid" function can take in multiple vectors as input, and will return a meshgrid with the specified number of dimensions. However, it is most commonly used for two-dimensional grids.

5. Are there any alternative methods to creating a meshgrid in MATLAB?

Yes, there are alternative methods to creating a meshgrid in MATLAB. One option is to use the "ndgrid" function, which creates a multidimensional grid. Another option is to manually create a matrix of coordinates using the "repmat" and "reshape" functions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
831
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Calculus and Beyond Homework Help
Replies
14
Views
602
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
Back
Top