# Interpolating 2D data with missing values

I have a list (21 x 21) containing values. I want to eliminate slots containing zeros by interpolating the nearest values and overwriting the zeros. How do I use the Mathematica's ListInterpolation function to do that?

• And ... what have you tried? – Dr. belisarius Jan 22 '13 at 4:22
• @belisarius I tried using ListInterpolation, using the list as parameter, but doesn't work.. comes up with error – Mun Jan 22 '13 at 4:26
• @belisarius ListInterpolation allows me to input an array of values from which it interpolates the data. But is there a way of interpolating the data based on the nearest values? the top, bottom, side values? Thank you – Mun Jan 22 '13 at 4:29
• You should post your code. – Vitaliy Kaurov Jan 22 '13 at 4:52
• Related question: mathematica.stackexchange.com/questions/4776/… – Eli Lansey Jan 22 '13 at 15:34

You have to use Interpolation because there it is possible to give values which do not lie on a structured grid. Let's create some test data which has zeroes in it

data = Table[ Sin[x] + Cos[y], {x, 0, Pi/2, Pi/2/29.}, {y, 0, Pi/2, Pi/2/29.}]*
RandomInteger[{0, 1}, {30, 30}];
ListPlot3D[data] Now you attach the position of every value to it so you get tuples of the form {{x,y},value} and Select only those, where value is not zero. This can then be interpolated

data2 = Select[Flatten[MapIndexed[{#2, #1} &, data, {2}], 1],
Last[#] != 0 &];
ip = Interpolation[data2];
Plot3D[ip[y, x], {x, 1, 30}, {y, 1, 30}] Note, that with such data you always have an InterpolationOrder of 1.

Yes, this is indeed a problem and you have to decide what to do. First you have to understand the issue. Let's assume the following grid with values, where all white cells are zero and have to be interpolated When you look at cell {3,1}, you see that such a situation is not a problem, because the value can be interpolated since it is place in between the cells {1,2} and {1,4}.

On the other hand, the cell {5,5} is a problem, because no surrounding cells with values are left which can be used for interpolation. Therefore, what you have to ensure is, that you have values on all 4 corners of your matrix. When they got selected out because they were zero, then you have to fill them with values. When they are indeed zero, and got kicked out accidentally, then you have to fill them back in.

You can ignore the corners when you Select non-zero values by changing the condition in the end

{ny, nx} = Dimensions[data];
data2 = Select[Flatten[MapIndexed[{N@#2, #1} &, data, {2}], 1],
Last[#] != 0 &&
(First[#] =!= {1, 1} || First[#] =!= {1, nx} ||
First[#] =!= {ny, 1} || First[#] =!= {ny, nx}) &];

• thanks for the answer. I have one slight issue. When I remove the zeros and interpolate, I get some errors; More precisely: if for example the position {1,1} had a zero, I get an error message saying "{1,1} cannot be interpolated, so extrapolation is used" And I get a fairly higher value than the surroundings. Also after a few of these errors, it says "Interpolation output suppressed". Any idea how to fix that? – Mun Feb 1 '13 at 0:57
• @Mun See my update in the post. – halirutan Feb 1 '13 at 4:20
• that's a very good remark. Thanks for that. I'll have a look at it – Mun Feb 1 '13 at 4:33
• When I use the method above to do interpolation, I get errors: Unstructured grid interpolation order reduced to 1 and coerced to machine precision. In version 9, it seems to do it but when running the code in version 7, I get an error message and stops. Can you please help me in finding out how to interpolate unstructured data properly? – Mun Feb 19 '13 at 0:48