Z must be a matrix, not a scalar or vector.
13 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2023-3-1
What exactly is your question?
size(omega2)
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
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
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
2023-3-2
编辑:Alexandra Roxana
2023-3-2
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
2023-3-2
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
2023-3-2
Thanks! That helps me.
In the first comment, what does 35 signify?
omega2 = omega2(randperm(numel(omega2),35));
Star Strider
2023-3-2
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
2023-3-16
编辑:Alexandra Roxana
2023-3-16
@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
2023-3-16
‘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
2023-3-16
编辑:Alexandra Roxana
2023-3-16
@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.
Star Strider
2023-3-16
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
2023-3-16
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
2023-3-16
编辑:Star Strider
2023-3-16
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
2023-3-16
编辑:Alexandra Roxana
2023-3-16
@Star Strider It looks great to me. This is what I wanted.
Many, many thanks!
Alexandra Roxana
2023-3-18
编辑:Alexandra Roxana
2023-3-18
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.
Alexandra Roxana
2023-3-18
编辑:Alexandra Roxana
2023-3-18
Actually I divided by 2 the steps dxf, dy, dxs to double the number of points.
Star Strider
2023-3-18
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
2023-3-18
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
2023-3-18
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
2023-3-18
Thank you very much for all your help! I will try to make it work if I try to change the domain values.
Star Strider
2023-3-18
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
2023-3-21
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
2023-3-21
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
2023-3-21
编辑:Alexandra Roxana
2023-3-21
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.
Star Strider
2023-3-21
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
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!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)