"Error using mesh: Z must be a matrix, not a scalar or vector."
1 回表示 (過去 30 日間)
古いコメントを表示
Bonjour ! Je m'adresse à vous parce que je desespere un peu.
J'ai une genre de fonction mais qui fonctionne avec des boucles for et if, et je veux construire un tracé de surface avec mesh donc j'ai utilisé la fonction comme ça :
>> [X,Z] = meshgrid(x,z);
>> mesh(X,Z, velocity);
avec la boucle et tous les autres détails au dessus (elle est vraiment longue donc je vous passe les détails) sauf que j'obtiens l'erreur :
"Error using mesh
Z must be a matrix, not a scalar or vector."
Alors effectivement mon Z (enfin velocity dans le cas présent) est composé d'une ligne et de nombreuses colonnes mais je ne comprend pas en quoi c'est censé poser problème ?
J'espère beaucoup que vous pourrez m'aider ! Merci.
6 件のコメント
Dyuman Joshi
2023 年 10 月 20 日
There are undefined parameters in the code, vertical and depthf, thus I can not run it.
However, looking at the 2nd for loop, it seems that you are following a relation to define velocity. Could you provide the mathematical defintion for it?
velocities=[0 4.6 5.1 6.3 8.3];
thicknesses=[0 3 4 6 8];
%for loop
depththicknesses=zeros(size(thicknesses));
depthlayer=0;
for i=1:length(thicknesses)
depthlayer=depthlayer+thicknesses(i);
depththicknesses(i)=-depthlayer;
end
depththicknesses
%Vectorized code
-cumsum(thicknesses)
回答 (2 件)
Mathieu NOE
2023 年 10 月 23 日
hello
I figured out that there are a few issues in the code , so I commented the lines where I found a bug or missing / not declared / initialized variable
at least you get a working plot but don't ask me if that is supposed to be the technically answering the question
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1518671/image.png)
velocities=[0 4.6 5.1 6.3 8.3];
thicknesses=[0 3 4 6 8];
depththicknesses=zeros(size(thicknesses));
depthlayer=0;
for i=1:length(thicknesses)
depthlayer=depthlayer+thicknesses(i);
depththicknesses(i)=-depthlayer;
end
horizontal=40;
depth=sum(thicknesses);
gridvert=0.01;
gridhoriz=0.02;
x=0:gridhoriz:horizontal;
z=-depth:gridvert:0;
[X,Z] = meshgrid(x,z);
% velocity=zeros(length(vertical),horizontal/gridhoriz); % "vertical"
% value or array not defined , should it be replaced by z ?
% velocity=zeros(length(z),horizontal/gridhoriz);
velocity=zeros(length(z),horizontal/gridhoriz + 1); % the second dimension was not initialized with the right value
for i=2:length(depththicknesses)
for j=1:length(z)
% if depththicknesses(i-1)>=Z(j) && depthf(j)>=depththicknesses(i)
% what i depthf ?? (replaced by z)
if depththicknesses(i-1)>=Z(j) && z(j)>=depththicknesses(i)
velocity(j)=velocities(i);
end
end
end
figure(1);
mesh(X,Z, velocity);
ylabel('Depth (km)');
xlabel('Horizontal (km)');
zlabel('Velocity (km/s)');
title('Velocity Model using mesh');
Ishaan
2023 年 10 月 23 日
編集済み: Ishaan
2023 年 10 月 23 日
Hi Érine,
Comme je ne suis pas de langue maternelle française, je vais essayer de répondre à cette question en anglais. Merci pour votre compréhension.
I understand that you are facing the following error while using the "mesh" function in MATLAB:
"Z must be a matrix, not a scalar or vector"
The error is caused due to the incompatible dimenstions of the third arguement ("velocity" in this case) corresponding to the first two arguments given to the "mesh" funciton.
Initially, if "x" and "y" have dimensions "1 x A" and "1 x B" respectively, then both the outputs "X" and "Y" from "meshgrid" will have dimensions "B x A". The same is demonstrated in the code snippet below:
x = 1:20;
y = 1:40;
size(x)
size(y)
[X,Y] = meshgrid(x,y);
size(X)
size(Y)
Now, if these "X", "Y", and a third variable "Z" are to be the three arguments to "mesh" function, then if, "X" and "Y" (outputs from "meshgrid" function) have dimensions "B x A", ensuring that "Z" also has the same dimensions will resolve the error at your end, which is illustrated by the following code snippet:
Z = X.*X + Y.*Y; % notice the usage of "X" and "Y" instead of "x" and "y"
size(Z)
mesh(X,Y,Z)
You can also refer to the following MathWorks documentation page for more details on the usage of "mesh" function:
Hope this helps,
Ishaan Mehta
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!