Vectors not the same length

1 次查看(过去 30 天)
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

采纳的回答

Cris LaPierre
Cris LaPierre 2021-4-12
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 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by