Linear interpolation in rows of matrix

3 Ansichten (letzte 30 Tage)
Gabor Pauer
Gabor Pauer am 27 Jan. 2020
Kommentiert: Star Strider am 28 Jan. 2020
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 Kommentar
Mohammad Sami
Mohammad Sami am 27 Jan. 2020
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 27 Jan. 2020
The fillmissing function (R2016b and later) will do what you want:
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 Kommentare
Gabor Pauer
Gabor Pauer am 28 Jan. 2020
Wow Star Strider, many thanks again, I tried it on my data and it works :)
Just one more thing: this solution does not take into account, that if the first or last value(s) are missing, then leave it empty (or put there a NaN), which was solved in the first method with the EndValues command.
(e.g. in my example we dont have measurement data for vehicle1 at the last measurement point, where distance is 7.
So First column of DSI should be: 50,54,58,62,62,64,NaN
Third column should be: NaN, 60, 62, 62,66, NaN, NaN )
Can you maybe solve this easily? I don't want to waste your time, many thanks friend once again!
Star Strider
Star Strider am 28 Jan. 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by