Azzera filtri
Azzera filtri

Error Code: Unable to perform assignment because the left and right sides have a different number of elements

5 visualizzazioni (ultimi 30 giorni)
Calculation of acceleration versus time using numerical derivatives
a=zeros(1,length(t)); % Create the velocity array - initially filled with zeros
a(1)=(v(2)-v(1))/(t(2)-t(1)); % first velocity point - method 1
a(2:end-1)=(v(3:end)-v(1:end-2))./(t(3:end)-t(1:end-2)); % method 3
Unable to perform assignment because the left and right sides have a different number of elements.
a(end)=(v(end)-v(end-1))./(t(end)-t(end-1)); % last point - method 2
  3 Commenti
CECILIA
CECILIA il 12 Set 2023
They are the same and I plotted both the position graph and velocity graph on the same script just before so I don't understand why it's different.
Walter Roberson
Walter Roberson il 12 Set 2023
I suggest you use the debugger,
dbstop on error
and run the code. When it stops, examine the size() of each variable mentioned in the line, and examine the size() of the left-side expression and the size() of the right-side expression.
Your code would have a problem if v and t are the same size but different orientations. For example if v is a column vector when t is a row vector.

Accedi per commentare.

Risposte (1)

the cyclist
the cyclist il 12 Set 2023
Modificato: the cyclist il 12 Set 2023
My best guess is that while your t and v have the same number of elements (which would allow plotting them against each other), one is a row vector while the other is a column vector.
For example, this code works:
N = 7;
t = rand(N,1);
v = rand(N,1);
a=zeros(1,length(t)); % Create the velocity array - initially filled with zeros
a(1)=(v(2)-v(1))/(t(2)-t(1)); % first velocity point - method 1
a(2:end-1)=(v(3:end)-v(1:end-2))./(t(3:end)-t(1:end-2)); % method 3
a(end)=(v(end)-v(end-1))./(t(end)-t(end-1)); % last point - method 2
but this code gives the error you got
N = 7;
t = rand(N,1);
v = rand(1,N);
a=zeros(1,length(t)); % Create the velocity array - initially filled with zeros
a(1)=(v(2)-v(1))/(t(2)-t(1)); % first velocity point - method 1
a(2:end-1)=(v(3:end)-v(1:end-2))./(t(3:end)-t(1:end-2)); % method 3
Unable to perform assignment because the left and right sides have a different number of elements.
a(end)=(v(end)-v(end-1))./(t(end)-t(end-1)); % last point - method 2

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by