Removing all NaN rows from a 2D matrix
Peeyush Khare
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:
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
July 18, 2023 at 04:05 am - Permalink
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.
July 18, 2023 at 05:25 am - Permalink
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 ;-)
July 18, 2023 at 05:26 am - Permalink
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. ;)
July 18, 2023 at 05:31 am - Permalink
In reply to Assuming that there are no… by ChrLie
Neat one liner.
July 18, 2023 at 06:43 am - Permalink
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!
July 19, 2023 at 12:22 am - Permalink
I guess you are using Igor 8 then. Here is Igor 8 compatible code:
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
July 19, 2023 at 01:11 am - Permalink
In reply to I guess you are using Igor 8… by chozo
I think this should work too, but I didn't test it:
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
July 19, 2023 at 02:20 am - Permalink
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)
July 19, 2023 at 05:41 am - Permalink