Clear Filters
Clear Filters

Interpolating between two columns in a matrix

5 views (last 30 days)
I have been given a data set in the form of a matrix. However some of the columns do not contain any data, they contain NaN (Not a Number). I have to interpolate between the data in the columns on the left and right of the missing data to get values for the missing data. I am not sure how to code this.

Accepted Answer

Voss
Voss on 17 Mar 2024
Edited: Voss on 17 Mar 2024
% generate a matrix with NaNs like yours:
M = rand(10,10);
M(randi(100,1,40)) = NaN
M = 10×10
0.0860 NaN 0.2929 0.0244 NaN NaN NaN NaN 0.1565 0.1559 0.8762 0.5397 0.1635 0.9825 0.7246 0.2477 NaN 0.6308 0.7632 NaN 0.6176 0.5404 NaN 0.2835 0.0218 NaN 0.4985 0.8669 0.4208 NaN 0.9064 0.8730 0.5421 0.6180 0.7841 NaN NaN 0.8407 NaN 0.4283 0.1815 0.3167 0.3499 NaN 0.1629 0.7482 NaN 0.7024 NaN 0.4220 0.7402 0.8328 NaN 0.6384 0.8992 NaN 0.3286 0.8999 NaN 0.6723 0.8472 0.5059 NaN 0.3425 0.0312 NaN 0.3294 0.4326 NaN 0.7510 NaN NaN NaN NaN 0.2865 0.5090 0.3695 0.2821 0.4773 NaN NaN 0.0105 0.0583 NaN 0.9986 NaN 0.1857 NaN 0.0611 0.4373 0.4712 NaN 0.5547 0.4074 NaN 0.8051 0.8785 NaN 0.5805 0.8098
% fill the NaNs using linear interpolation/extrapolation between columns (dimension 2):
M = fillmissing(M,'linear',2)
M = 10×10
0.0860 0.1895 0.2929 0.0244 0.0508 0.0773 0.1037 0.1301 0.1565 0.1559 0.8762 0.5397 0.1635 0.9825 0.7246 0.2477 0.4392 0.6308 0.7632 0.8956 0.6176 0.5404 0.4120 0.2835 0.0218 0.2602 0.4985 0.8669 0.4208 -0.0253 0.9064 0.8730 0.5421 0.6180 0.7841 0.8029 0.8218 0.8407 0.6345 0.4283 0.1815 0.3167 0.3499 0.2564 0.1629 0.7482 0.7253 0.7024 0.5622 0.4220 0.7402 0.8328 0.7356 0.6384 0.8992 0.6139 0.3286 0.8999 0.7861 0.6723 0.8472 0.5059 0.4242 0.3425 0.0312 0.1803 0.3294 0.4326 0.5918 0.7510 -0.6036 -0.3811 -0.1586 0.0640 0.2865 0.5090 0.3695 0.2821 0.4773 0.6724 -0.0374 0.0105 0.0583 0.5285 0.9986 0.5921 0.1857 0.1234 0.0611 0.4373 0.4712 0.5129 0.5547 0.4074 0.6062 0.8051 0.8785 0.7295 0.5805 0.8098

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!