what causes "Z must be a matrix, not a scalar or vector" in mesh()

3 ビュー (過去 30 日間)
Astudent
Astudent 2019 年 5 月 28 日
編集済み: David 2019 年 5 月 28 日
I know this was asked before, but the answers don't really answer the question, and only provide solutions.
Why do I get the error "Z must be a matrix, not a scalar or vector" with the following script:
x = linspace(-2, 2);
y = linspace(-2, 2);
z = -(x.^2 + 2.*y.^2).*exp(1-x.^2-y.^2);
figure
subplot(3, 2, 1), mesh(x, y, z);
I saw some answers talking about capital letters and so on, but I don't really understand how they affect the situation.

採用された回答

Adam Danz
Adam Danz 2019 年 5 月 28 日
編集済み: Adam Danz 2019 年 5 月 28 日
From the documentation for mesh() (<-- the first place you should go to understand the inputs to a function):
" If X and Y are vectors, length(X) = n and length(Y) = m, where [m,n] = size(Z)"
That means z is a matrix of size (m,n) where m is the length of y and n is the length of x.
In this example z has a size of (4x6); x has a length of 6 and y has a length of 4. For every (x,y) coordinate, there is a value for z. That's why z must be a matrix if x and y are vectors and that's why the dimensions of z agree with the dimensions of x and y.
  1 件のコメント
Astudent
Astudent 2019 年 5 月 28 日
Clear explanation. thank you!

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2019 年 5 月 28 日
The z input to mesh must have at least two rows and two columns. It contains the height of points in the mesh (you can think of this as the height of tent poles for a party tent), and you don't really have a mesh if z is a vector (you would have something that looks more like a fence or wall, which is not a tent) or a scalar (one tent pole also does not a tent make.)
To put it another way, the z input must return true when you call ismatrix on it (it can't have 3 or more dimensions) but must return false when you call isvector on it. [A scalar is a very short vector.]

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by