Change in odometer.
조회 수: 5 (최근 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!