Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
And ... what have you tried? –  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
3  
You should post your code. –  Vitaliy Kaurov Jan 22 '13 at 4:52
2  
Related question: mathematica.stackexchange.com/questions/4776/… –  Eli Lansey Jan 22 '13 at 15:34
add comment

1 Answer

up vote 4 down vote accepted

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]

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}) &];
share|improve this answer
    
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
1  
@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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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