File Exchange

image thumbnail


version 1.0 (1.07 KB) by

One dimensional Interpolation over NaNs.

14 Ratings



View License

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');

Comments and Ratings (17)


the best

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


Petter (view profile)

Nice and short

Chad Greene

Chad Greene (view profile)

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:


Shmuel (view profile)


Ashley (view profile)

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


Joel (view profile)

Worked great for me!!!

Tina Hegarty

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

David D



% Test
Matrice= [ 99 2 3 0 6;
4 5 99 0 0;
7 0 8 99 NaN]
% Matrice bidon pour voir position index
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;

% 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);

% 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);
it's not working can, someone help me

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

Girts Murans

Carlos Adrian Vargas Aguilera

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 Release
MATLAB 6.5 (R13)

Inspired: repnan, interp1gap

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today