Linear interpolation in rows of matrix
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi!
I have vehicle speed measurement data in the following form (small example):
Distance (from the point of measurement, in metres): 1, 2, 3, 4, 5, 6, 7
Speed of vehicle1 at these points (in km/h): 50, 54, NaN, 62, 62, 64, NaN
Speed of vehicle2 at the points: 40, NaN, NaN, NaN, 48, 54, 60
Speed of vehicle3 at the points: NaN, 60, 62, 62, 66, NaN, NaN
Speed of vehicle 4 at the points: 44, 47, NaN, 55, NaN, NaN, 58
...
I would like to get a value instead of NaN with linear interpolation. BUT I dont want to interpolate if NaNs are the first/last elements of the measurement.
Needed result of example above: [50 54 58 62 62 64 NaN; 40 42 44 46 48 54 60; NaN 60 62 62 66 NaN NaN, 44 47 51 55 56 57 58].
Im absolutely a novice user of MATLAB, but I can not handle this problem in Excel, so I really need your help. How to insert these data (in a matrix form or how?), and how to get a good result with that I can work further (there are quite a lot vehicles in this database :S) ?
Thank you in advance
Gábor
댓글 수: 1
Mohammad Sami
2020년 1월 27일
If your data is in a table. use the function fillmissing.
Use the option 'EndValues','none' to avoid filling in the first and the last values.
채택된 답변
Star Strider
2020년 1월 27일
D = [1, 2, 3, 4, 5, 6, 7];
V1 = [50, 54, NaN, 62, 62, 64, NaN];
V2 = [40, NaN, NaN, NaN, 48, 54, 6];
V3 = [NaN, 60, 62, 62, 66, NaN, NaN];
V4 = [44, 47, NaN, 55, NaN, NaN, 58];
Vs = [V1;V2;V3;V4];
Vout = fillmissing(Vs,'linear',2, 'EndValues','none')
producing:
Vout =
50 54 58 62 62 64 NaN
40 42 44 46 48 54 6
NaN 60 62 62 66 NaN NaN
44 47 51 55 56 57 58
As requested.
댓글 수: 4
Star Strider
2020년 1월 28일
As always, my pleasure!
There were no NaN values in those data. If there were, the best approach would likely be to use the same essential logic to separate the segments, and then use fillmissing instead of interp1 on each segment.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!