Solving MATLAB Meshgrid: A Beginner's Guide

  • Thread starter Thread starter cookiemnstr510510
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion focuses on the MATLAB function meshgrid, which creates matrices from two input vectors, allowing for element-wise operations. The user defines vectors for length L=[1 3 5] and width W=[2 4 6 8], and utilizes [X,Y]=meshgrid(L,W) to generate matrices that represent all combinations of these values. The resulting matrices X and Y facilitate element-wise multiplication using X.*Y, yielding the outer product of the two arrays. The discussion clarifies that meshgrid effectively pairs each element of L with every element of W.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with vectors and matrices
  • Knowledge of element-wise versus matrix multiplication
  • Concept of outer product in linear algebra
NEXT STEPS
  • Explore MATLAB's ndgrid function for higher-dimensional grids
  • Learn about element-wise operations in MATLAB
  • Study the concept of outer products in linear algebra
  • Investigate practical applications of meshgrid in data visualization
USEFUL FOR

Students, educators, and professionals in engineering, data analysis, or any field utilizing MATLAB for mathematical modeling and data manipulation.

cookiemnstr510510
Messages
162
Reaction score
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
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   Reactions: cookiemnstr510510
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   Reactions: cookiemnstr510510 and marcusl

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
1
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 10 ·
Replies
10
Views
5K