Hello,
I am using the meshgrid function to try and make a grid for my surface plots. My x,y, and z arrays are length 102, 42, and 102 respectively. But when I run [X,Y,Z] = meshgrid(x,y,z) the output has the wrong dimensions. X, Y and Z are 42 x 102 x 102 not 102 x 42 x 102. Can someone help me understand what is going wrong with this?
I tried playing around with the order and if I have [X,Y,Z] = meshgrid(y,x,z) it gives the correct output size of 102 x 42 x 102. For now I can run the code using this alternative order but I would like to know why it is making the wrong size.
Thanks

1 件のコメント

Stephen23
Stephen23 2017 年 6 月 22 日
編集済み: Stephen23 2017 年 6 月 22 日
That is the correct behavior of meshgrid. You should be using ndgrid instead.
(And learn to navigate to the bottom of the help page, where you will find a list of "See Also" functions. You would have found ndgrid yourself quite simply by scrolling to the bottom of the page)

サインインしてコメントする。

 採用された回答

Jan
Jan 2017 年 6 月 21 日
編集済み: Jan 2017 年 6 月 21 日

1 投票

Why do you assume that the output is "wrong"? It is exactly how defined in the documentation:
[X,Y,Z] = meshgrid(x,y,z) returns 3-D grid coordinates defined by the
vectors x, y, and z. The grid represented by X, Y, and Z has size
length(y)-by-length(x)-by-length(z).
Well, length(y)-by-length(x)-by-length(z) sounds strange, I agree. But the function works as advertised. Note: Never trust your expectations, what a function returns, but trust only the documentation. Intuition is fine, but programming languages have been designed by human. ;-)
A similar problem is gradient:
[FX,FY] = gradient(F)
Now FX is along the "horizontal direction", FY the "vertical direction". Sounds okay. But in
[FX,FY,FZ] = gradient(F)
the 1st output concerns the 2nd dimension, the 2nd output the 1st one and the 3rd output the 3rd one. Brrr.

5 件のコメント

Steven Lord
Steven Lord 2017 年 6 月 21 日
Consider using ndgrid for this type of operation instead. See the Tips section on that page.
Gabriele Curcio
Gabriele Curcio 2023 年 10 月 13 日
編集済み: Gabriele Curcio 2023 年 10 月 13 日
but why that "strange" output and not a "normal" one? just for curiosity
Steven Lord
Steven Lord 2023 年 10 月 13 日
I wouldn't call either meshgrid or ndgrid "strange" or "normal". They simply use different conventions for how they arrange their data. See this Answers post for more information.
Stephen23
Stephen23 2023 年 10 月 13 日
編集済み: Stephen23 2023 年 10 月 13 日
"but why that "strange" output and not a "normal" one? just for curiosity"
The reason is due to humans and the graphics they like to look at: when given a matrix of data, many use-cases interpret the horizontal direction as the horizontal axes... and the vertical direction as the vertical axes. This is most likely what you would expect when you display a matrix as an image (otherwise displaying a matrix of data in the command window would be transposed to how it would look when displayed as an image... ouch!). But consider this: the horizontal axes is mathematically defined as the 2nd matrix dimension, while the vertical axes as the 1st matrix dimension. So humans get the first two dimensions mixed up: they prefer the independent-axes as the X-axes and the dependent-axes as the Y-axes... but note that this means the 2nd and 1st matrix dimensions respectively. Ouch.
In essence it is due to an inconsistency between the mathematical definition of matrices/arrays (i.e. NDGRID) and graphics/image/plotting expectations (i.e. MESHGRID). Note that this is not specific to MATLAB: it applies to all graphics apps/libraries and all mathematical apps/libraries.
You can find some explanations here:
This not just something that MATLAB has to contend with, see the indexing option here:
and another blog, apparently written by someone with a graphics background:
This has been discussed many times before:
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 16 日
Nice explaination, @Stephen23!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

タグ

質問済み:

2017 年 6 月 21 日

コメント済み:

2023 年 10 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by