# Do Matlabs 3D plots swap axes?

17 visualizzazioni (ultimi 30 giorni)
Jannis il 28 Mag 2018
Modificato: Jan il 30 Mag 2018
Dear all,
I am very confused by Matlab's 3D plotting functions (surf, plot3). Consider the following MWE:
>> [X,Y] = meshgrid(1:20,1:20);
>> Z = sin(X) + cos(Y);
>> Z(1,end)=10;
>> surf(X,Y,Z)
>> xlabel('x')
>> ylabel('y')
I expected the command Z(1,end)=10; to set a peak at the first value of the x axis and the last value of the y axis. However, it generates the following plot, in which the axes seem to be flipped. Please point me to my error. I can reproduce this behavior for plot3 as well.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Stephen23 il 29 Mag 2018
Modificato: Stephen23 il 29 Mag 2018
The confusion occurs because X and Y do not correspond to rows and columns (i.e. dimensions 1 and 2) of the plotted matrix, which is what you are assuming. In MATLAB, as is the standard in mathematics, the first dimension corresponds to the rows, the second the columns, the third the pages, etc. of an array/matrix:
which means, for example, that a matrix with size 2x3 has two rows and three columns. Note how these do NOT correspond to X and Y!
However some people prefer to think of the first dimension as being left/right and the second up/down (i.e. X and Y), but note that this swaps the roles of the first two dimensions (because how they are stored does not match how they are displayed)! In any case, to make it easy for users wanting to treat X and Y as the first two dimensions, functions like meshgrid support this: you can see in dpb's example that X actually increases along the second dimension, whereas Y increases along the first dimension.
The solution is to either
• change all mathematics to consider the columns as the first dimension and rows as the second (unlikely), or
• understand that in MATLAB (like all mathematics) matrix rows are encoded by the first dimension and columns by the second, and this means when you think in terms of X and Y you will need to transpose their roles in your matrices (much easier).
Note that meshgrid is limited to 2/3 dimensions, because this is typically what people want when they work in terms of X and Y (and Z). But there is also a completely consistent function ndgrid, which does not swap role the first two dimensions, and operates up to as many dimensions as you want to. If you look carefully at some examples of ndgrid and meshgrid you will see that the first two dimensions of meshdrigd's output are the odd ones out (basically swapped around), whereas in every case the Nth output of ndgrid increases along the Nth dimension.
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Stephen23 il 29 Mag 2018
Modificato: Stephen23 il 29 Mag 2018
"For cartesian coordinates, mathematics uses x first, to mean distance left/right"
I guess the tendency to list the independent variable first is the cause of putting X first when discussing plots, which restricts the coordinates to having X horizontally. Perhaps if we used a different notation for writing maths (e.g. output first) this would be different and we would consider the dependent variable as the X axis. Or is there a tendency for humans to prefer the horizontal axis as the independent variable, similar to how most scripts are written horizontally? In any case, clearly the order of matrix indices and the order of coordinate systems developed independently from one another.
"I gather that historically tables were not arranged that way, but that it changed when people moved from scroll format to folio (cut page) format."
Sounds feasible. I was just trying to hunt down some good sources on this history of the matrix indexing standard: do you happened to know any?
Jan il 30 Mag 2018
Modificato: Jan il 30 Mag 2018
@Stephen: Ha, cool. I gave you 2 votes!
The servers are slow today. It seems to be easy to cause uncaught some racing condition. We have some threads with 2 accepted answers also. This means, that Matlab Answers is not an ACID prove database. Well, the state of the art is something else.

Accedi per commentare.

### Più risposte (1)

dpb il 28 Mag 2018
Not the way meshgrid is laid out -- look at a small enough space you can see it easily (and also not symmetric so can tell who's who in the zoo):
>> [X,Y]=meshgrid(1:4,1:6)
X =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Y =
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
>>
The location you're looking for is Z(end,1) instead of Z(1,end)
##### 5 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Jan il 29 Mag 2018
The unexpected order of outputs concerns gradient also.
dpb il 29 Mag 2018
Modificato: dpb il 29 Mag 2018
I didn't say was intuitive, only that's the way meshgrid has always worked... :)
I don't know, but I strongly suspect it exists as is simply owing to historical reasons that it was introduced that way initially probably without a tremendous amount of thought regarding the order peculiarity.
NDGRID came along somewhat later albeit still a long time back but did fix the inconsistency.
Probably would be worthy of a documentation improvement suggestion to make specific note of the difference in MESHGRID() similar to that in NDGRID that specifically notes the difference. Although MESHGRID describes the output, it isn't easy to decipher from just the words...
Have to admit I've never really thought about it too much; the general purpose is to simply display a surface and the specifics of how the ordering was arranged for the purpose hasn't ever really cropped up as an issue...

Accedi per commentare.

### Categorie

Scopri di più su Annotations in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by