이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Z must be a matrix, not a scalar or vector.
조회 수: 10 (최근 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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1311480/image.png)
.
댓글 수: 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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1326690/image.png)
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]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1332105/image.png)
... 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에서 Surface and Mesh Plots에 대해 자세히 알아보기
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 (한국어)