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

조회 수: 28 (최근 30일)
Alexandra Roxana
Alexandra Roxana 2023년 3월 1일
댓글: Star Strider 2023년 3월 21일
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
Rik
Rik 2023년 3월 1일
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 2023년 3월 1일
Look at your X,Y, and omega2 variables. The size of all of them should be the same when using the surf function.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2023년 3월 1일
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
Alexandra Roxana
Alexandra Roxana 2023년 3월 21일
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 2023년 3월 21일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by