Matrix dimensions must agree.

4 visualizaciones (últimos 30 días)
Daniel Rincón
Daniel Rincón el 15 de Feb. de 2018
Comentada: Star Strider el 15 de Feb. de 2018
Help me, Matlab print the next error.
Matrix dimensions must agree. Error in Grafica (line 4) Vt = (3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)).*sin(pi.*n.*t);
This is my code:
clc; clear all; close all
n = 0:1:10;
t = 0:0.01:10;
Vt = (3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)).*sin(pi.*n.*t);
plot (Vt)
end

Respuesta aceptada

Star Strider
Star Strider el 15 de Feb. de 2018
Yes, they must.
However you can get round that restriction by using the meshgrid function:
n = 0:1:10;
t = 0:0.01:10;
[N,T] = meshgrid(n,t);
Vt = @(n,t) (3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)).*sin(pi.*n.*t);
plot (t, Vt(N,T))
grid on
Experiment to get the result you want.
  2 comentarios
Daniel Rincón
Daniel Rincón el 15 de Feb. de 2018
Thank you, it works perfectly
Star Strider
Star Strider el 15 de Feb. de 2018
As always, my pleasure.

Iniciar sesión para comentar.

Más respuestas (1)

Jonathan Chin
Jonathan Chin el 15 de Feb. de 2018
n.*t
n and t don't match dimensions, when you do .* you are trying to do an element wise multiplication, since they are not the same length it gives you that error.
I think this is what you are trying to accomplish
n = [0:1:10].';
t = 0:0.01:10;
Vt = bsxfun(@times,sin(pi.*n*t),(3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)));
plot (Vt.')
I created your sine wave for every n against the array t, this creates a 11x1001 matrix, with a sin wave on each row corresponding to n
sin(pi.*n*t)
Then every column gets element wised multiplied by a 11x1 column using bsxfun(@times...)
(3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)) %11x1 column
Finally I transpose the matrix so plot can show each individual sine wave

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!

Translated by