Borrar filtros
Borrar filtros

Change in odometer.

5 visualizaciones (últimos 30 días)
Ireedui Ganzorig
Ireedui Ganzorig el 16 de Mzo. de 2020
Comentada: Ireedui Ganzorig el 16 de Mzo. de 2020
Hello MATLAB community
I am trying to find the Change in Odometer, and below is my code. Odometer here is a bunch of data in a column. I feel like my i should start from 1, but when I make the i equals to 1, I am getting an error says indices are not the same. Can anyone help me fix this problem by making the i equals to 1?
Odometer = load('40815Odometer.csv'); % in miles
for i = 2 : length(Odometer+1)
changeInOdometer = Odometer(i) - Odometer(i-1)
end
ChangeInOdometer = changeInOdometer - Odometer(1)

Respuesta aceptada

Sriram Tadavarty
Sriram Tadavarty el 16 de Mzo. de 2020
Hi Ireedui,
It looks like you wanted to get the difference of two consecutive odometer readings.
The diff function will help in this case. Here is the documentation link for diff function: https://www.mathworks.com/help/matlab/ref/diff.html
% For example Odometer has values [5 9 6 3];, Change in odometer readings will be 4 -3 -3
ChangeInOdometer = diff(Odometer);
% if you wanted even to store the first value,
ChangeInOdometer = [Odometer(1) diff(Odometer)];
To work off your code
% If you only wanted the diff
Odometer = load('40815Odometer.csv'); % in miles
for i = 2 : length(Odometer)
changeInOdometer(i-1) = Odometer(i) - Odometer(i-1)
end
% If you want to store the first value
Odometer = load('40815Odometer.csv'); % in miles
changeInOdometer(1) = Odometer(1);
for i = 2 : length(Odometer)
changeInOdometer(i) = Odometer(i) - Odometer(i-1)
end
Hope this helps.
Regards,
Sriram
  1 comentario
Ireedui Ganzorig
Ireedui Ganzorig el 16 de Mzo. de 2020
Hello Sriram,
Thank you very much. This really helped me A LOT.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays 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