Created
March 9, 2021 07:18
-
-
Save Hio-Been/3f653d0ce899e6a88d4d911846efc0f3 to your computer and use it in GitHub Desktop.
MATLAB function for interpolating 2d matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mat_interp = interp_nan2d( mat, kernel_size ) | |
% | |
% MATLAB function for interpolating 2d matrix | |
% | |
% Interpolating nan in 2D matrix | |
% Hio-Been Han, hiobeen.han@kaist.ac.kr | |
% 2021-03-09. | |
% | |
shape = size(mat); | |
if nargin < 2,kernel_size = ceil( mean(shape)/50 );end | |
mat_interp = mat; | |
[nan_x, nan_y] = find( isnan(mat )); | |
for idx1 = 1:length(nan_x) | |
for idx2 = 1:length(nan_y) | |
xIdx = nan_x(idx1)-kernel_size:nan_x(idx1)+kernel_size; | |
yIdx = nan_y(idx2)-kernel_size:nan_y(idx2)+kernel_size; | |
xIdx(find(xIdx<1))=[];yIdx(find(yIdx<1))=[]; | |
xIdx(find(xIdx>shape(1)))=[]; yIdx(find(yIdx>shape(2)))=[]; | |
box = mat(xIdx, yIdx); | |
mat_interp(xIdx, yIdx) = nanmean(box(:)); | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment