File Exchange

## Inpainting nan elements in 3-d

version 1.0.0.0 (297 KB) by John D'Errico

### John D'Errico (view profile)

An extension of inpaint_nans to 3 dimensions

Updated 25 Aug 2008

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 (2020). Inpainting nan elements in 3-d (https://www.mathworks.com/matlabcentral/fileexchange/21214-inpainting-nan-elements-in-3-d), MATLAB Central File Exchange. Retrieved .

dominik

### dominik (view profile)

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

dominik

### dominik (view profile)

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?

Paul

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,
Cheers.

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.

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:

http://matlabwiki.mathworks.com/Citing_Files_from_the_File_Exchange

Pierre-Julien Trombe

### Pierre-Julien Trombe (view profile)

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

Alvaro Canivell

### Alvaro Canivell (view profile)

Exactly what I was looking for !

Georgina

### Georgina (view profile)

Nikola Toljic

Excellent. Thanks.

Tim Rae

Worked extremely well!

Thankyou so much, John

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

Carlos

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