Change in odometer.

3 次查看(过去 30 天)
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)

采纳的回答

Sriram Tadavarty
Sriram Tadavarty 2020-3-16
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 个评论
Ireedui Ganzorig
Ireedui Ganzorig 2020-3-16
Hello Sriram,
Thank you very much. This really helped me A LOT.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by