Matrix dimensions must agree
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (2)
Robert
el 15 de Dic. de 2016
Editada: Robert
el 15 de Dic. de 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 comentarios
Walter Roberson
el 15 de Dic. de 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
el 15 de Dic. de 2016
Editada: Carolina Homem de Gouveia
el 15 de Dic. de 2016
Walter Roberson
el 15 de Dic. de 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 comentario
Robert
el 15 de Dic. de 2016
Editada: Robert
el 15 de Dic. de 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.
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!