Matrix dimensions must agree.

2 views (last 30 days)
Daniel Rincón
Daniel Rincón on 15 Feb 2018
Commented: Star Strider on 15 Feb 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

Accepted Answer

Star Strider
Star Strider on 15 Feb 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 Comments
Daniel Rincón
Daniel Rincón on 15 Feb 2018
Thank you, it works perfectly
Star Strider
Star Strider on 15 Feb 2018
As always, my pleasure.

Sign in to comment.

More Answers (1)

Jonathan Chin
Jonathan Chin on 15 Feb 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

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!