現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Z must be a matrix, not a scalar or vector.
5 ビュー (過去 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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)