Index Out Of Range and Logic in Wave arithmatic

So I've got a nice little image, and that image has some NaN values that I need to eliminate to be able to do things like ffts, sums, ect
I've traditionally done this kind of thing in a nested for-loop, where I'll check each element index by index, and if I find a NaN, I'll replace that with a neighboring value (our imaging resolution is lower than our pixel size).

This is slow. So I'm trying to replace it with this line of code:



TheTrap[][] = (TheTrap(p)(q)!=NaN)*TheTrap(p)(q) +(TheTrap(p)(q)==NaN && p>0)*TheTrap(p-1)(q) +(TheTrap(p)(q)==NaN && p==0)*TheTrap(p+1)(q)



The first bit returns the original image if it has a finite value. The second bit replaces any NaNs found with the value of the pixel to the left if there exists a pixel to the left. And the final bit replaces any NaNs found on the leftmost part of the image with the value of the pixel immediately to the right.

What I get when I run is an "index is out of range" message, because in the second bit the program is ostensibly trying to multiply 0*TheTrap(-1)(q). Is there a way to get around this without resorting to a for loop?
1) Comparing with NaN always returns false, per IEEE standard. You need (numtype(TheTrap[p][q])!=0) or (numtype(TheTrap[p][q]) == 0) instead.

2) How about MatrixFilter NaNZapMedian?

3) Use a subrange to limit the left-hand side:
Variable rows = DimSize(TheTrap, 0)
Variable cols = DimSize(TheTrap, 1)
TheTrap[1,rows-1][1,cols-1] = ...
TheTrap[0][1,cols-1] = ...
etc.


John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
The following function does what you want without generating an out-of-range error. However, you might want to look into the MatrixFilter solution as suggested.
Function ReplaceNans(w)
    Wave w
    w[][] = numtype(w[p][q])==0 ? w[p][q] : w[p-1+2*(p==0)][q]
End

Also note that when the leftmost (p=0) AND the adjacent pixel to the right (p+1) contain NaNs the function is unable to overwrite these.
Quote:
Also note that when the leftmost (p=0) AND the adjacent pixel to the right (p+1) contain NaNs the function is unable to overwrite these.

And MatrixFilter documentation claims that NaNZapMedian "automatically cycles through matrix until all NaNs are gone".

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Thank you all very much! Pretty much everything every one of you said was new to me.