Matrix dimensions must agree

1 vue (au cours des 30 derniers jours)
Carolina Homem de Gouveia
Carolina Homem de Gouveia le 15 Déc 2016
Modifié(e) : Robert le 15 Déc 2016
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.

Réponses (2)

Robert
Robert le 15 Déc 2016
Modifié(e) : Robert le 15 Déc 2016
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 commentaires
Walter Roberson
Walter Roberson le 15 Déc 2016
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 le 15 Déc 2016
Modifié(e) : Carolina Homem de Gouveia le 15 Déc 2016
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.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 15 Déc 2016
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 commentaire
Robert
Robert le 15 Déc 2016
Modifié(e) : Robert le 15 Déc 2016
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by