Code covered by the BSD License  

Highlights from
naninterp

5.0

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

naninterp

by E. Rodriguez

 

05 Aug 2005 (Updated 01 Dec 2005)

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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
06 Oct 2005 ho ge

Good job!

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,

26 Jun 2008 Yasmin M

I love you!!!

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

13 Oct 2008 Girts Murans  
20 Jan 2009 Deepak

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

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

21 Feb 2010 Tina Hegarty

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

Please login to add a comment or rating.
Updates
01 Dec 2005

added keywords

Tag Activity for this File
Tag Applied By Date/Time
matrices E. Rodriguez 22 Oct 2008 07:55:04
nan E. Rodriguez 22 Oct 2008 07:55:04
nans E. Rodriguez 22 Oct 2008 07:55:04
interpolation E. Rodriguez 22 Oct 2008 07:55:05
interpolate E. Rodriguez 22 Oct 2008 07:55:05
outliers E. Rodriguez 22 Oct 2008 07:55:05
gaps E. Rodriguez 22 Oct 2008 07:55:05
gap E. Rodriguez 22 Oct 2008 07:55:05
holes E. Rodriguez 22 Oct 2008 07:55:05
interpolation Mason Freed 08 Jun 2009 13:39:39
gaps Ian Howat 03 Nov 2009 17:24:16
gap Xiaoling 20 Dec 2011 13:38:07

Contact us at files@mathworks.com