plotting matrix with the command plot and meshgrid. Please clarify that to me.

6 views (last 30 days)
Suppose I create a simple matrix y = [ 2 4; 6 1] and then when I plot it using plot like, plot(y,'*') I tried to understand how it works and I realize the x-axis are the matrix rows and the y-axis is the elements of the matrix. Am I right? if so, why it works like that? why not x-axis gives the column for instance? when I plot 2 matrix such as let x be x = y.^2 and then plot(x,y,'*') in this case the elements of x and y are put together.... is it the same when using other plot commands like mesh, surf,plot3 and so on... ?
**meshgrid
suppose I have x = linspace(-3,3) ; y = x ; I thought it'd be the same doing this z = x.^2 + y.^2 ; [xx yy zz ] = meshgrid(x,y,z) or this [xx yy] = meshgrid(x,y) ; z = xx.^2 + yy.^2 ... this last z I thought it should be the same guy as zz but actually they don't even have the same size.. What's the difference between it? ( defining z in terms of x and y and put everyone into the meshgrid AND defining z in terms of xx and yy which were put into the meshgrid before z was set) ...
Thank you for helping me. I'm a beginner sorry if those questions are stupid.

Accepted Answer

dpb
dpb on 3 Apr 2014
Edited: dpb on 6 Apr 2014
No, your first example is not right -- per the documentation for plot, the columns of Y when no X is given are individual lines down each column and the X is 1:size(Y,2) or 1:nColumns in Y.
WHY? "Because" that's the way it is by TMW definition. It's a logical choice in Matlab because array storage is column-major (that is, increasing first by row, then by column so a given column is a set of consecutive values in memory).
See
doc plot % and friends for more details and examples
ADDENDUM:
In the second, x and y are two vectors and z is the result on those N points where N=length(x) so is also a vector of length N.
meshgrid does what the name implies--it takes the two input vectors x any y and fills out a grid of corresponding size NxN where there's a value at each.
The easiest way to see the difference is to use a vector that is short enough that the result shows easily in the command window--try
x=-1:1; y-x;
[X,Y]=meshgrid(x,y)
Study the output until you see the pattern...

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!