Filter löschen
Filter löschen

"Error using mesh: Z must be a matrix, not a scalar or vector."

1 Ansicht (letzte 30 Tage)
Érine
Érine am 20 Okt. 2023
Kommentiert: Érine am 24 Okt. 2023
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 20 Okt. 2023
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?
Also, you can vectorize the first loop using cumsum -
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
depththicknesses = 1×5
0 -3 -7 -13 -21
%Vectorized code
-cumsum(thicknesses)
ans = 1×5
0 -3 -7 -13 -21
Érine
Érine am 20 Okt. 2023
Mince, pardon pour les paramètres non-definis, j'ai modifié des fonctions et j'ai dû les oublier !
Pour la relation, la consigne était la suivante :
Créez un modèle de vitesse 2D composé de quatre couches. La première couche a une vitesse P de 4,6 km/s, la deuxième couche une vitesse P de 5,1 km/s, la troisième couche a une vitesse de 6,3 km/s et la quatrième couche une vitesse de 8,3 km/s. L'épaisseur des couches est de 3 km, 4 km, 6 km et 8 km. La dimension horizontale du modèle est de 40 km. Utilisez un espacement de grille dans le sens vertical de 10 m et un espacement de grille dans le sens horizontal de 20 m.
Utiliser les boucles for et if me paraissaient le plus cohérent, mais peut-être est-ce qu'il y avait un meilleur moyen

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Mathieu NOE
Mathieu NOE am 23 Okt. 2023
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
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
Ishaan am 23 Okt. 2023
Bearbeitet: Ishaan am 23 Okt. 2023
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)
ans = 1×2
1 20
size(y)
ans = 1×2
1 40
[X,Y] = meshgrid(x,y);
size(X)
ans = 1×2
40 20
size(Y)
ans = 1×2
40 20
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)
ans = 1×2
40 20
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

Tags

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!