Removing all NaN rows from a 2D matrix

Hi, 

 

I have a 2D matrix that has some fully NaN rows in it. I need to get rid of these rows. Kindly advise. ZapNaNs doesn't work since it only applies to 1D waves. 

I can't seem to figure this out without writing multiple lines of code. 

 

Thanks a ton!

Peeyush 

While still technically 'multiple lines of code', here is what I came up with:

function zapNanRows(wave in)
    variable i
    for (i=DimSize(in,0)-1; i>-1; i--)
        MatrixOP/Free isNan = zapNaNs(row(in,i))
        if (numpnts(isNan) == 0)
            DeletePoints/M=0 i,1,in
        endif
    endfor
end

 

Are the NaNs anywhere else? Just in contiguous rows? If so, redimension the matrix to a 1D wave, zap the NaNs, and convert back to a matrix. Simply be sure to restore to the number of columns, for example a 10x15 matrix that zaps 15 points becomes a 9x15 matrix. Unfortunately, I am not where I can write a function example to do this.

Assuming that there are no other NaN's except in "blank rows" you can use a MatrixOP one-liner:

1) use zapNaNs --> gives 1D NaN-zapped wave

2) use Redimension and figure out the new number of rows

together:

MatrixOP/O out = Redimension(zapNaNs(in), numRows(zapNaNs(in))/numCols(in), numCols(in))

 

EDIT:

Seems JJ was a tiny bit faster ;-)

 

This is what I tried first. Note that this will only work if you don't have NaNs anywhere else other than in the rows you are trying to get rid of. If there is even one NaN anywhere else your are scrambling the whole data with the one-liner. Neat, indeed, but use at your own risk. ;)

Thank you so much all for your response! I appreciate it very much.. 

There can also be NaNs in other rows at some places, hence to play safe, I tried executing chozo's code. However, it is throwing a syntax error highlighting "zapNaNs". 

Please advise. 

Thanks! 

I guess you are using Igor 8 then. Here is Igor 8 compatible code:

function zapNanRows(wave in)
    variable i
    for (i=DimSize(in,0)-1; i>-1; i--)
        Duplicate/Free/RMD=[i][] in, isNan
        Redimension/N=(numpnts(isNan),0) isNan
        WaveTransform zapNans, isNan
        if (numpnts(isNan) == 0)
            DeletePoints/M=0 i,1,in
        endif
    endfor
end

 

In reply to by chozo

I think this should work too, but I didn't test it:

function zapNanRows(wave in)
    int i
    int numCols = DimSize(in, 1)
    for (i=DimSize(in,0)-1; i>-1; i--)      
        WaveStats/RMD=[i,i][]/Q/M=1 in
        if (V_numNaNs == numCols)
            DeletePoints/M=0 i,1,in
        endif
    endfor
end

Edit: i forgot to type the wavename in the wavestats line

Look at MatrixOP zapNaNs(). 

Start by getting the number of columns:

Variable cols=DimSize(srcWave,1)

Now execute:

MatrixOP/o/free clean=zapNaNs(srcWave)   // use /free if you are running in a function

This results in a 1D wave so follow up with Redimension() as in

MatrixOP/O cleanMatrix=ReDimension(clean,numpoints(clean)/cols,cols)