File Exchange

image thumbnail

Inpainting nan elements in 3-d

version (297 KB) by John D'Errico
An extension of inpaint_nans to 3 dimensions


Updated 25 Aug 2008

View License

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.

Cite As

John D'Errico (2021). Inpainting nan elements in 3-d (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

Feng Cheng


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


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?



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,

Sean de

Thank you John for this wonderful contribution!

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

John D'Errico

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:

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?

Alvaro Canivell

Exactly what I was looking for !


Nikola Toljic

Excellent. Thanks.

Tim Rae

Worked extremely well!

Thankyou so much, John

Carlos Adrian Vargas Aguilera

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


MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Inspired by: inpaint_nans

Inspired: PIVsuite

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!