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?

  • 1
    $\begingroup$ And ... what have you tried? $\endgroup$ Jan 22, 2013 at 4:22
  • $\begingroup$ @belisarius I tried using ListInterpolation, using the list as parameter, but doesn't work.. comes up with error $\endgroup$
    – Mun
    Jan 22, 2013 at 4:26
  • $\begingroup$ @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 $\endgroup$
    – Mun
    Jan 22, 2013 at 4:29
  • 3
    $\begingroup$ You should post your code. $\endgroup$ Jan 22, 2013 at 4:52
  • 2
    $\begingroup$ Related question: mathematica.stackexchange.com/questions/4776/… $\endgroup$
    – Eli Lansey
    Jan 22, 2013 at 15:34

1 Answer 1


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}];

Mathematica graphics

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}]

Mathematica graphics

Note, that with such data you always have an InterpolationOrder of 1.

Update regarding your comment

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

Mathematica graphics

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}) &];
  • $\begingroup$ 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? $\endgroup$
    – Mun
    Feb 1, 2013 at 0:57
  • 1
    $\begingroup$ @Mun See my update in the post. $\endgroup$
    – halirutan
    Feb 1, 2013 at 4:20
  • $\begingroup$ that's a very good remark. Thanks for that. I'll have a look at it $\endgroup$
    – Mun
    Feb 1, 2013 at 4:33
  • $\begingroup$ 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? $\endgroup$
    – Mun
    Feb 19, 2013 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.