Interpolating NaN values in a matrix

4 views (last 30 days)
luke
luke on 21 Mar 2024
Commented: luke on 21 Mar 2024
Hi All,
I am struggling with figuring out how I can use the interp1 function to linearly interpolate NaN values I have in a matrix. Say I have a matrix of dimensions 60x262 where in some cases I have multiple NaN values consecutively following from one another. I need to linearly interpolate these values. Any help would be greatly appreciated as I am still relatively new to matlab. below shows an example snippit of what the data involves.
-0.828613088652154 -0.826980732195421 -0.745832994170700 -0.837225160883488 NaN NaN -0.837114555898372 -1.05110236873639 -0.722208231882682
4.18144347979596 4.19446029799520 4.08802043519884 4.18419237580195 NaN NaN 4.17362356334361 4.18245686518250 4.19128594336630
4.09152405469652 4.09275379468674 NaN 4.09538191166424 NaN NaN 4.08455856869444 4.09203705160658 4.09228949595775
4.08634155982602 4.08818643022896 NaN 4.09096836770649 NaN NaN 4.08492266864219 4.09274350933265 4.08847941329410
5.07399449632967 5.06211813602457 NaN 5.07085029962670 NaN NaN 5.04164860981005 5.04551480261894 5.03276042592886
4.68644787557317 4.69023528651558 NaN 4.68973364637793 NaN NaN 4.68784423465079 4.69096351892401 4.66146546447165
3.56536907590693 3.56844880494464 NaN 3.57312639760225 NaN NaN 3.55120891370827 3.54807604084560 3.51040739603457
5.06176892358968 5.07473468800532 NaN 5.04070296179379 NaN NaN 5.05685629184707 5.05999710637877 5.06785151261156
3.35036679231905 3.34747514113894 NaN 3.36101893534686 NaN NaN 3.32837566913524 3.32877399145182 3.27174735672586
2.31689012217437 2.31439512603361 NaN 2.31559487774751 NaN NaN 2.23320260448975 2.23240257763717 2.13986411760340
  2 Comments
DGM
DGM on 21 Mar 2024
Is this 2D data, or is each column representative of a different variable, as in a table or spreadsheet?
fillmissing() might work for the latter case
regionfill() might work for the 2D case
luke
luke on 21 Mar 2024
2D data so each row is associated with a single transect and the columns are associated with consecutive days. I will try these now, thank you

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 21 Mar 2024
interp1 isn't the right tool for this job. I recommend using fillmissing (with the dim input argument if you want to interpolate across the rows) or fillmissing2 instead.
  3 Comments
Steven Lord
Steven Lord on 21 Mar 2024
Right, I didn't phrase that very well. I probably should have said along the rows.
luke
luke on 21 Mar 2024
This worked, appreciate the help

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!