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

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 Comments

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?
Look at your X,Y, and omega2 variables. The size of all of them should be the same when using the surf function.

Sign in to comment.

 Accepted Answer

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 Comments

I can attach the file; I thought having to put more than 2000 lines in the question would be quite problematic.
The thing is, I have a system with particular points as in the first figure. From the solution, I want to surf only a part of it, that is, omega2. And I wonder how to make it a matrix so I can use surf. Is there another way to make it look like it, maybe another command, not surf?
I have absolutely no idea what you are doing.
The only way I can create ‘omega2’ as a surf plot is to do this:
Z=inv(M);
sol=Z*L;
omega1=sol(NTN+1:2*NTN-2*NNAB);
omega2=sol(2*NTN-2*NNAB+1:3*NTN-4*NNAB);
No2 = numel(omega2)
om2fact = factor(No2)
[X,Y]=meshgrid(0:om2fact(1)-1, 0:om2fact(2)-1);
figure(2)
F = scatteredInterpolant(X(:),Y(:),omega2); % Create 'scatteredInterpolant' Object
omega2 = F(X,Y); % Interpolate
surf(X,Y,omega2)
colormap(turbo)
Stopping here.
.
Thanks! That helps me.
In the first comment, what does 35 signify?
omega2 = omega2(randperm(numel(omega2),35));
That is the number of elements in the original individual ‘[X,Y]’ matrices.
All the original vector sizes presented to scatteredInterpolant have to match.
@Star Strider How can I use this same command without randperm?
I'm using [X,Y]=meshgrid(a:b,c:d); I would like also the meshgrid to be more dense, with [X,Y]=meshgrid(a:0.5:b,c:0.5:d) maybe?
How can I use this same command without randperm?
I only used randperm here because the vector sizes have to match, and they don not in your posted code.
I would like also the meshgrid to be more dense, with [X,Y]=meshgrid(a:0.5:b,c:0.5:d) maybe?
If the vector sizes match, that could work. The vector sizes must always match for this approach to work.
.
@Star Strider Oh, so randperm just takes random variables from omega2? omega2 is not randomly generated in my program. The thing is, that using randperm the plot will always look different.
You can take any subset of ‘omega2’ you want.
The only absolute requirement is that the vector lengths always have to match. You can do that by increasing the sizes of the meshgrid output matrices or truncating ‘omega2’ to fit them.
OK, the thing is that I don't want to truncate omega2, I did that from 187 to 117 and it doesn't look the best. I would like to use the entire solution so that would mean maximizing the step.
How can I take a subset without using randperm?
You can use the entire vector if you are willing to make some compromises.
This is the only way I can think of to produce the result you apparently want using the entire vector —
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);
fctr = factor(numel(omega2)); % Prime Factors
a_b = linspace(a, b, fctr(1)); % Create Vector
c_d = linspace(c, d, fctr(2)); % Create Vector
[X,Y]=meshgrid(a_b,c_d); % Create Matrices
Omega2 = griddata(X(:), Y(:), omega2, X, Y); % Interpolate To Calculate Matrix
figure(2)
surf(X,Y,Omega2)
xlabel('a:b')
ylabel('c:d')
zlabel('omega2')
The online Run feature is currently down for scheduled maintenance (according to the pop-up notice that appears when I try to run this), so I ran it on MATLAB Online to be certain it worked. It does. I cannot determine if it produces the resullt you want.
EDIT — (16 Mar 2023 at 18:48)
Ran code.
.
@Star Strider It looks great to me. This is what I wanted.
Many, many thanks!
As always, my pleasure!
Thank you!
One question if you don't mind: what happens if omega2 hasn't the dimension the product of 2 prime numbers? I tried to change b and d so to have more points on the domain and the dimension isn't anymore written as a product of prime numbers so it can't show any figure anymore.
Actually I divided by 2 the steps dxf, dy, dxs to double the number of points.
The only problem is if the number is prime, since prime numbers can only be integer-divided by themselves and 1. For the others, the factor function still works.
If the length of ‘omega2’ is a prime, it may be necessary to truncate it to a non-prime length and then use the appropriate factoring techniques to calculate the vectors. This approach assumes that a prime length is allowed, and sets the length of one vector to the prime number and the other vector to 1.
The immediate problem appears to be when there are more than two prime factors to the length. That simply requires some choices, for example —
a=0;
b=4;
c=0;
d=6;
omega2 = 1:randi(1E+4);
omega2Len = numel(omega2)
omega2Len = 3852
fctr = factor(numel(omega2)); % Prime Factors
primeFactors = fctr
primeFactors = 1×5
2 2 3 3 107
if numel(primeFactors) > 2 % More Than Two Prime Factors
max2 = maxk(primeFactors,2); % Select Two Highest Factors
Len_a_b = prod(max2);
Len_c_d = numel(omega2)/Len_a_b;
elseif numel(primeFactors) == 1 % Length Is Prime
Len_a_b = primeFactors;
Len_c_d = 1
else % Only Two Prime Factors
Len_a_b = primeFactors(1);
Len_c_d = primeFactors(2);
end
VectrLens = [Len_a_b Len_c_d]
VectrLens = 1×2
321 12
a_b = linspace(a, b, Len_a_b); % Create Vector
c_d = linspace(c, d, Len_c_d); % Create Vector
Check = numel(a_b) * numel(c_d)
Check = 3852
This is one option, however there are others. It all depends on how you want to deal with those situations. Experiment with this approach to get the result you want.
.
Thank you again for your patience! It didn't run though but I will watch carefully where I might have made a mistake.
I have tried using this:
figure(2)
U=repmat(omega2,1,numel(omega2));
[X,Y]=meshgrid(linspace(a,b,numel(omega2)),linspace(c,d,numel(omega2)));
surf(X,Y,U')
but I don't think it's right.
I do not believe it is either.
The lengths of the vectors need to match the appropriate dimensions of ‘omega2’ so that everything works.
My previous Comment is my best effort to solve that problem. I cannot devise any other way of doing it.
.
Thank you very much for all your help! I will try to make it work if I try to change the domain values.
As always, my pleasure!
I am not able to devise a method that willl always produce the ‘correct’ independent variable matrices that match an arbitrary-length vector.
Usually, the result is the other way round — create the independent variable matrices first, and then create the dependent variable array from them. It would be best if you could take that approach with your vector.
I have managed to come with a different and shorter approach:
Omega2=zeros(NNAD,NNAB+2);
for i=2:NNAD-1
for j=2:NNAB+1
Omega2(i,j)=omega2((i-2)*NNAB+j-1);
end
end
figure(3)
[X,Y]=meshgrid([a:dxs:a+g (a+g)+dxf:dxf:b-g (b-g)+dxs:dxs:b],c:dy:d);
C = 1 + (X <= a+g-dxs | X >= b-g);
surf(X,Y,Omega2,C);
colormap([1 0 0; 0 0 1]);
That appears to provide a much better solutiono to the plotting problem.
I am not certain where to put that code in the context of the earlier code, however I would like to do that to test it to see how it works and to see if I could improve its efficiency.
The vectors X and Y were built with the steps as in figure 1, Omega2 must have 0 on the first and last row, and the first and last column since the points on the first and last base on figure 1 are not taken into account.
O.K. So If I understand correctly —
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=zeros(NNAD,NNAB+2);
for i=2:NNAD-1
for j=2:NNAB+1
Omega2(i,j)=omega2((i-2)*NNAB+j-1);
end
end
figure(3)
[X,Y]=meshgrid([a:dxs:a+g (a+g)+dxf:dxf:b-g (b-g)+dxs:dxs:b],c:dy:d);
C = 1 + (X <= a+g-dxs | X >= b-g);
surf(X,Y,Omega2,C);
colormap([1 0 0; 0 0 1]);
... and with that, I finally see the essence of what you are doing.
I appreciate the follow-up.
.
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!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!