Matrix dimensions must agree.

2 Ansichten (letzte 30 Tage)
Daniel Rincón
Daniel Rincón am 15 Feb. 2018
Kommentiert: Star Strider am 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

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jonathan Chin
Jonathan Chin am 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

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by