Vectors not the same length

조회 수: 1 (최근 30일)
Roshaan Rauf
Roshaan Rauf 2021년 4월 12일
답변: Cris LaPierre 2021년 4월 12일
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