Code covered by the BSD License  

Highlights from
naninterp

5.0

5.0 | 9 ratings Rate this file 43 Downloads (last 30 days) File Size: 1.07 KB File ID: #8225

naninterp

by

 

05 Aug 2005 (Updated )

One dimensional Interpolation over NaNs.

| Watch this File

File Information
Description

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
--------------------------------------------------------------------------

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
31 Jul 2013 Gwenolé LE PACHE  
20 Jan 2013 Shmuel  
22 Jun 2012 Ashley

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

13 Apr 2012 Joel

Worked great for me!!!

21 Feb 2010 Tina Hegarty

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

27 Feb 2009 David D

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

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

20 Jan 2009 Deepak Trivedi

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

13 Oct 2008 Girts Murans  
30 Jun 2008 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

26 Jun 2008 Yasmin M

I love you!!!

29 Nov 2005 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,

06 Oct 2005 ho ge

Good job!

Contact us