Vectors not the same length

1 visualización (últimos 30 días)
Roshaan Rauf
Roshaan Rauf el 12 de Abr. de 2021
Respondida: Cris LaPierre el 12 de Abr. de 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

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 12 de Abr. de 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

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by