Vectors not the same length
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Roshaan Rauf
am 12 Apr. 2021
Beantwortet: Cris LaPierre
am 12 Apr. 2021
i cant seem to fix my problem i am calculationg acceleration from a column of time and speed and later on i need to plot some graphs so the vectors need to be the same length,however when i calculate acceleration, there are two less values then time.
dataAccel = []; %Create empty array to store acceleration values in
for i = 1:1:length(Time) %Create a loop that continues up until the last value of time in the column
if i < length(Time)-1%Stop error occuring from the the code continuing on longer then the array
ChangeinTime = Time(i+1)- Time(i); % Calculates the time difference
ChangeinSpeed = Speed(i+1)-Speed(i); % Calculates the speed difference
Acceleration = ChangeinSpeed/ChangeinTime; %Calculates Acceleration
dataAccel = [dataAccel;Acceleration]; %Stores acceleration in empty array
end
end
0 Kommentare
Akzeptierte Antwort
Cris LaPierre
am 12 Apr. 2021
If there are less items in Speed then Time, then your if statement should use length(Speed) instead. However, that may not always be the case. Perhaps use the min length from either?
for i = 1:min(length(Time),length(Speed)) %Create a loop that continues up until the last value of time in the column
ChangeinTime = Time(i+1)- Time(i); % Calculates the time difference
ChangeinSpeed = Speed(i+1)-Speed(i); % Calculates the speed difference
Acceleration = ChangeinSpeed/ChangeinTime; %Calculates Acceleration
dataAccel = [dataAccel;Acceleration]; %Stores acceleration in empty array
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Line Plots 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!