フィルターのクリア

Matrix dimensions must agree

1 回表示 (過去 30 日間)
Carolina Homem de Gouveia
Carolina Homem de Gouveia 2016 年 12 月 15 日
編集済み: Robert 2016 年 12 月 15 日
Here is my code
t = 0:pi/300:pi;
F = sin(t).*(sin(5*t)./(5*t));
% b) Polar
r = abs(F);
% c) Discrete
t1 = 0:pi/30:pi;
dis_F = sin(t1).*(sin(5*t1)./(5*t1));
figure(1);
subplot(3,1,1), plot(t,F), title('Cartesian');
subplot(3,1,2), polar(t,r), title('Polar');
subplot(3,1,3), stem(t1,dis_F), title('Discrete');
% 2. Tridimensional
% a) Cartesian coordinates
t2 = 0:pi/100:pi;
p2 = 0:2*pi/100:2*pi;
[t2,p2] = meshgrid(t2,p2);
F2 = (sin(10*sin(t2).*sin(p2)-3)./((10*sin(t2)).*sin(p2)-3)).*((sin(10*cos(t2)-5))./(10*cos(t2)-5));
% b) Spherical coordinates
x = r.*sin(t2).*cos(p2);
y = r.*sin(t2).*sin(p2);
z = r.*cos(t2);
The rest of my code doesn't run because of my x. It says the matrix dimensions must agree but I don't see how I can change it according to the error. Please help. Thanks.

回答 (2 件)

Robert
Robert 2016 年 12 月 15 日
編集済み: Robert 2016 年 12 月 15 日
Sorry i looked into it further. p2 and t2 are the same size
the problem is r r is 1 by 301
p2 and t2 are 101X101
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 12 月 15 日
In R2016b at least, t2 and p2 are the same size. It would still be a good idea to use linspace though.
Carolina Homem de Gouveia
Carolina Homem de Gouveia 2016 年 12 月 15 日
編集済み: Carolina Homem de Gouveia 2016 年 12 月 15 日
t2 = linspace(0,pi,100);
p2 = linspace(0,2*pi,100)
[t2,p2] = meshgrid(t2,p2);
F2 = (sin(10*sin(t2).*sin(p2)-3)./((10*sin(t2)).*sin(p2)-3)).*((sin(10*cos(t2)-5))./(10*cos(t2)-5));
% b) Gráfico em coordenadas esféricas x = r.*sin(t2).*cos(p2);
I used linspace but it stayed the same. Besides that the t2 and p2 variables show on the workspace as 101*101double, both of them. Thank you.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2016 年 12 月 15 日
Your r was created in your first step based upon t = 0:pi/300:pi; which is length 301. Your t2 and p2 are created in your second step based upon t2 = 0:pi/100:pi; and p2 = 0:2*pi/100:2*pi; which are each length 101, and then you meshgrid() each of those so your t2 and p2 are 101 x 101 by the time you try to combine x = r.*sin(t2).*cos(p2); which is then (1 by 301) .* (101 by 101) .* (101 by 101), which fails.
If you were to create t as the same length as t2 starts, so both length 101, then you would have
(1 by 101) .* (101 by 101) .* (101 by 101)
In any version earlier than R2016b that would not be permitted. In R2016b the first term would be automatically replicated to 101 by 101, giving you (101 by 101) .* (101 by 101) .* (101 by 101) which would be legal. In versions before R2016b you would need to do the replication yourself, either by using repmat() or by using bsxfun()
  1 件のコメント
Robert
Robert 2016 年 12 月 15 日
編集済み: Robert 2016 年 12 月 15 日
I also saw the problem above with the R. I changed my comment above. Although walter beat me to it with a more comprehensive answer but yes the problem is the size of R Make it a habit to keep track of your variable type and size in the base workspace.

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by