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)
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!