**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

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

8 views (last 30 days)

Show older comments

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.

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

##### 2 Comments

### Accepted Answer

Star Strider
on 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 Comments

Alexandra Roxana
on 2 Mar 2023

Edited: Alexandra Roxana
on 2 Mar 2023

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?

Star Strider
on 2 Mar 2023

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.

.

Alexandra Roxana
on 2 Mar 2023

Thanks! That helps me.

In the first comment, what does 35 signify?

omega2 = omega2(randperm(numel(omega2),35));

Star Strider
on 2 Mar 2023

That is the number of elements in the original individual ‘[X,Y]’ matrices.

All the original vector sizes presented to scatteredInterpolant have to match.

Alexandra Roxana
on 16 Mar 2023

Edited: Alexandra Roxana
on 16 Mar 2023

@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?

Star Strider
on 16 Mar 2023

‘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.

.

Alexandra Roxana
on 16 Mar 2023

Edited: Alexandra Roxana
on 16 Mar 2023

Star Strider
on 16 Mar 2023

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.

Alexandra Roxana
on 16 Mar 2023

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?

Star Strider
on 16 Mar 2023

Edited: Star Strider
on 16 Mar 2023

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.

.

Alexandra Roxana
on 16 Mar 2023

Edited: Alexandra Roxana
on 16 Mar 2023

@Star Strider It looks great to me. This is what I wanted.

Many, many thanks!

Star Strider
on 16 Mar 2023

As always, my pleasure!

Thank you!

Alexandra Roxana
on 18 Mar 2023

Edited: Alexandra Roxana
on 18 Mar 2023

Alexandra Roxana
on 18 Mar 2023

Edited: Alexandra Roxana
on 18 Mar 2023

Actually I divided by 2 the steps dxf, dy, dxs to double the number of points.

Star Strider
on 18 Mar 2023

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.

.

Alexandra Roxana
on 18 Mar 2023

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.

Star Strider
on 18 Mar 2023

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.

.

Alexandra Roxana
on 18 Mar 2023

Star Strider
on 18 Mar 2023

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.

Alexandra Roxana
on 21 Mar 2023

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]);

Star Strider
on 21 Mar 2023

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.

Alexandra Roxana
on 21 Mar 2023

Edited: Alexandra Roxana
on 21 Mar 2023

Star Strider
on 21 Mar 2023

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.

.

Alexandra Roxana
on 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
on 21 Mar 2023

As always, my pleasure!

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)