Effacer les filtres
Effacer les filtres

Change in odometer.

1 vue (au cours des 30 derniers jours)
Ireedui Ganzorig
Ireedui Ganzorig le 16 Mar 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)

Réponse acceptée

Sriram Tadavarty
Sriram Tadavarty le 16 Mar 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 commentaire
Ireedui Ganzorig
Ireedui Ganzorig le 16 Mar 2020
Hello Sriram,
Thank you very much. This really helped me A LOT.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by