5.0 | 8 ratings Rate this file 21 Downloads (last 30 days) File Size: 297 KB File ID: #21214 Version: 1.0

Inpainting nan elements in 3-d


John D'Errico (view profile)


25 Aug 2008 (Updated )

An extension of inpaint_nans to 3 dimensions

| Watch this File

File Information

Every once in a while I get asked for a 3-d version of inpaint_nans, so I modified my existing 2-d code to run in 3 dimensions. This code has only two methods as options. I picked what I felt were the two most useful methods from inpaint_nans to extend to 3-d.

See the included published demos for examples of use.


Inpaint Nans inspired this file.

This file inspired Pi Vsuite and Inpaint Over Missing Data In 1 D, 2 D, 3 D,... N D Arrays.

MATLAB release MATLAB 7.5 (R2007b)
Other requirements This should run on older releases of MATLAB, I hope back as far as release 10, since the original code ran under release 10.
Tags for This File   Please login to tag files.

No tags are associated with this file.

Please login to add a comment or rating.
Comments and Ratings (12)
08 Oct 2013 dominik

And another question: My 3D variable is very large and contains many NaNs, all in one "corner of the cube". Actually im only interessted in inpainting the NaNs along the boarder between the NaN and the non-NaN area. Inpainting the whole cube takes ages and sometimes filles up the memory. Can I restrict inpaint to only a subset of the NaNs (those along the boarder in which i am interested)?

Comment only
02 Oct 2013 dominik

Hi, great function helps me a lot! in fact i need to inpaint a 4D variable. So far i did one loop breaking down the object to 3D but i noted that the function also accepts a 4D variable. The result looks reasonable.
Can this safely be done (i.e. can inpaint_nans3 be used for higher dimensions as well) or would u recommend not to do so?

Comment only
07 Jun 2013 Paul

Paul (view profile)

06 Jun 2013 Paul

Paul (view profile)

Hi John,

Thank you for your great function !

I would like to know if there is a way to avoid having negative numbers with the method 0.
I'm currently using your fonction in order to reshape volatility cubes. Sometimes the extrapolation algo returns negatives numbers , which i can't allow.

Could you help me on this matter,
Thank you for your help,

Comment only
08 Nov 2010 Sean de

Sean de (view profile)

Thank you John for this wonderful contribution!

Fast, well commented, and extremely useful for removal of noise in known locations in X-CMT images.

26 Oct 2010 John D'Errico

John D'Errico (view profile)

I discuss the two methods at some level in the original inpaint_nans submission. Perhaps that would be sufficient for you. Really, the default method in these tools is simply a boundary value solver for a PDE, defined over the domain of a hole in the supplied array. Since the function values are known around the perimeter of the hole, I simply formulate a simple partial differential equation problem on the interior. The solution involves discretizing the PDE on the lattice points of the array, which is a very natural thing to do on a regular grid. In the end, large scale, sparse linear algebra is employed, something that MATLAB does very well.

In the default method in inpaint_nans3, I have chosen Laplace's equation for the PDE. This has the virtue of making for a stable, robust, smooth solution, as well as providing a well behaved extrapolant for external holes. Had I used something else here (the more general Navier-Stokes comes to mind) the inpainting solution would now have been far less stable, far less robust, etc. And any intelligent extrapolation would be nearly impossible.

The second method in inpaint_nans3 is a spring analogue. This is something I originally put in on a lark, expecting it to have very limited utility. (I was wrong about that - as multiple people have pointed it out to be quite useful to them.) It formulates the problem in a mechanical way, connecting each node to its neighbors using "springs". Since a mechanical spring resists deformation by storing energy as a function of the square of the extension, the springs method tries not to extrapolate at all, but it still interpolates smoothly. In effect, it tries to do constant extrapolation, but to do so smoothly and in multiple dimensions. That springs problem is formulated as a large scale linear algebra one, much as the default PDE solution was solved. I don't have a reference for this method, it was simply my ad hoc solution to an interesting problem.

For those who simply wish to provide a citation, you can cite submissions on the FEX. Two schemes for doing so are posed here:


Comment only
25 Oct 2010 Pierre-Julien Trombe

Great. It helps a lot, not talking about the gain of time. :)
John: Would you have a references (articles, etc) on the methods you implemented?

16 Feb 2010 Alvaro Canivell

Exactly what I was looking for !

31 Jan 2010 Georgina  
19 Sep 2008 Nikola Toljic

Excellent. Thanks.

25 Aug 2008 Tim Rae

Worked extremely well!

Thankyou so much, John

25 Aug 2008 Carlos Adrian Vargas Aguilera

Thanks John, this give us hope for the GRIDFITN :) (just kidding). It's a great job.


Contact us