File Exchange

## naninterp

version 1.0 (1.07 KB) by

One dimensional Interpolation over NaNs.

4.53333
14 Ratings

Updated

One-line logically-indexed call to matlab function "interp1"

Fill nan-holes, fast and easy.

>> x = [1 2 NaN NaN 5 6];
>> naninterp(x)

ans =

1 2 3 4 5 6

Cubic interpolation by default, you should know how to change that :-)

--------------------------------------------------------------------------
function X = naninterp(X)
% Interpolate over NaNs
X(isnan(X)) = interp1(find(~isnan(X)), X(~isnan(X)), find(isnan(X)), 'cubic');
return
--------------------------------------------------------------------------

Will Feavyour

Dorfschafer

### Dorfschafer (view profile)

the best

laxminarayana pasupuleti

### laxminarayana pasupuleti (view profile)

its working...........nice

Petter

### Petter (view profile)

Nice and short

This function works as advertised, but lacks sufficient documentation. The function may also benefit from some simple input checks and functionality could be extended with an optional input of interpolation method.

Ashley, a well-documented function which fills NaNs in 2D can be found here: http://www.mathworks.com/matlabcentral/fileexchange/4551

Gwenolé LE PACHE

Shmuel

Ashley

### Ashley (view profile)

Does anyone know how to do this for a 2D matrix?

Joel

### Joel (view profile)

Worked great for me!!!

Tina Hegarty

### Tina Hegarty (view profile)

Thank you so much. I've spent so long trying to find a way to do exactly this! Great. :)

David D

### David D (view profile)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
clc

% Test
Matrice= [ 99 2 3 0 6;
4 5 99 0 0;
7 0 8 99 NaN]
% Matrice bidon pour voir position index
B=[1:1:15];
Z=reshape (B, 3, 5)
% Fin test

% Détection des cas recherchés
Where_NaN= isnan(Matrice);
idx_NaN= find (Where_NaN==1);
idx_0_99= find (Matrice==0 | Matrice== 99);

% Création d'une liste unique d'index des cas recherchés
% Noter que je fais la transposée de idx_0_99 car c'est une colonne de vals
% donc pour la concatenation je dois la mettre en ligne pour aller avec idx_NaN
Lst_NaN_0_99 = [idx_NaN idx_0_99.']

% Remplacement des valeurs par moyenne des valeurs qui entoure position.
% 2 cas particuliers: premières et dernières positions dans les colonnes.
% Les if sont là pour les détecter. Si 1ere position dans colonne, nouvelle
% valeur égale à valeur suivante. Si dernière position, valeur égale à
% valeur précédente.

% Détection format matrice
[lignes, colonnes]= size (Matrice);

% Cas classique (calcul moyenne)
idx_PosiIn= find (rem(Lst_NaN_0_99(:),lignes)~=0 & rem(Lst_NaN_0_99(:),lignes)~=1);
% Calculer si pas de cas non voulus côte à côte
if (Matrice (idx_PosiIn - 1)~= idx_PosiIn(:) & Matrice (idx_PosiIn + 1)~= idx_PosiIn(:))
Matrice (idx_PosiIn)= (Matrice (idx_PosiIn - 1) + Matrice (idx_PosiIn + 1))/2;
end

% Position en haut de colonne
idx_PosiUn= find (rem(Lst_NaN_0_99(:),lignes)==1);
% Calculer si pas de cas non voulus côte à côte
if Matrice (idx_PosiUn + 1)~= idx_PosiUn(:)
Matrice (idx_PosiUn)= Matrice (idx_PosiUn + 1);
end

% Position en bas de colonne
idx_PosiLast= find (rem(Lst_NaN_0_99(:),lignes)==0);
% Calculer si pas de cas non voulus côte à côte
if Matrice (idx_PosiLast - 1)~= idx_PosiLast(:)
Matrice (idx_PosiLast)= Matrice (idx_PosiLast - 1);
end
it's not working can, someone help me

Deepak Trivedi

### Deepak Trivedi (view profile)

Thanks! We need a N-D version of this too.

Girts Murans

Je je, nice little function, which I rader use as a script, but anyway. You should explain in the help that data should be a time series (i.e. equally spaced), you can use 'extrap' to interpolate edges, and use isnan only one time, and set the method as an optional function, use griddatan for N-dimensional array in order to make your function more interesting and is a nice test:

method = 'cubic';
extrap = {'extrap'};
T = reshape(1:length(X),size(X));
inan = isnan(X);
X(inan) = interp1(T(~inan),X(~inan),T(inan),method,extrap{:});

Anyway, good luck

Yasmin M

I love you!!!

moose o'donnell

Thanks a bunch... much simpler than what I was trying to write...

it took me a while to find this, so I'm including the search terms I was using: gap, fill, gapfill,

ho ge

Good job!

MATLAB 6.5 (R13)