Vectors not the same length
2 views (last 30 days)
Show older comments
Roshaan Rauf
on 12 Apr 2021
Answered: Cris LaPierre
on 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 Comments
Accepted Answer
Cris LaPierre
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Line Plots 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!