Z must be a matrix, not a scalar or vector.

26 vues (au cours des 30 derniers jours)
Alexandra Roxana
Alexandra Roxana le 1 Mar 2023
Commenté : Star Strider le 21 Mar 2023
I want to have a 3D plot with a solution vector but I have the error that Z must be a matrix.
a=0;
b=4;
c=0;
d=6;
g=1;
dxs=0.2;
dxf=0.25;
dy=0.5;
NNAB=(2*g)/dxs+(b-a-2*g)/dxf-1;
NNAD=(d-c)/dy+1;
NTN=NNAD*NNAB;
sol=rand(3*NTN-4*NNAB,1);
omega2=sol(2*NTN-2*NNAB+1:3*NTN-4*NNAB);
figure(2)
[X,Y]=meshgrid(a:b,c:d);
surf(X,Y,omega2)
Error using surf
Z must be a matrix, not a scalar or vector.
  2 commentaires
Rik
Rik le 1 Mar 2023
What exactly is your question?
size(omega2)
ans = 1×2
187 1
To use surf, the final variable should contain a height for each index of X and Y. This is clearly not the case. Why exactly did you pick these functions?
Cameron
Cameron le 1 Mar 2023
Look at your X,Y, and omega2 variables. The size of all of them should be the same when using the surf function.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 1 Mar 2023
It would be helpful to have the actual data rather than a ‘proxy problem’. The ‘Z’ vector should have the dame number of elements as the matrices. If so, it is then straightforward to interpolate them to form matrices, demonstrated here.
a=0;
b=4;
c=0;
d=6;
g=1;
dxs=0.2;
dxf=0.25;
dy=0.5;
NNAB=(2*g)/dxs+(b-a-2*g)/dxf-1;
NNAD=(d-c)/dy+1;
NTN=NNAD*NNAB;
sol=rand(3*NTN-4*NNAB,1);
omega2=sol(2*NTN-2*NNAB+1:3*NTN-4*NNAB);
omega2 = omega2(randperm(numel(omega2),35)); % Random Subset With The Same Number Of Elements As The Matrices
figure(2)
[X,Y]=meshgrid(a:b,c:d);
F = scatteredInterpolant(X(:),Y(:),omega2); % Create 'scatteredInterpolant' Object
omega2 = F(X,Y); % Interpolate
surf(X,Y,omega2)
colormap(turbo)
.
  25 commentaires
Alexandra Roxana
Alexandra Roxana le 21 Mar 2023
No problem, it was because of your idea of creating a matrix for plotting that I could come to this.
Thank you for your patience and time!
Star Strider
Star Strider le 21 Mar 2023
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by