Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: NaN filtering question
Replies: 2   Last Post: Jan 24, 2013 4:11 PM

 Messages: [ Previous | Next ]
 Derek Goring Posts: 3,922 Registered: 12/7/04
Re: NaN filtering question
Posted: Jan 24, 2013 2:00 PM

On Friday, January 25, 2013 7:19:08 AM UTC+13, Andy wrote:
> Hi All,
>
> I have some data from an instrument in a series of 1800 by 600 matrices, with NaNs where the instrument could not collect data. In some spots, there is a little bit of data surrounded by NaNs, which looks quite messy when plotted, and may not be accurate. I have some code that runs over the matrix and counts the number of NaNs around each point and then deletes them if surrounded by too many NaNs. It works, but I have committed the matlab cardinal sin of nesting for loops (gasp!) :
>
>
>
> for i=3:dimmX-2
>
> for j=3:dimmY-2
>
> sur=aod(i-2:i+2,j-2:j+2);
>
> surNan(i,j)=length(find(isnan(sur)==1));
>
>
>
> end
>
>
>
> end
>
> index=find(surNan>surMin);
>
> aod(index)=NaN;
>
>
>
> this of course is very slow, and I need to create averages from many of these matrices. Does anyone know of a better way to do this, or point me in a faster direction?
>
> Thanks, Andy.

Instead of length(find(isnan(sur)==1)) use simply sum(isnan(sur))
Logical indexing is faster than find. Only use find when you have to, so:
index=find(surNan>surMin);
should be
index=surNan>surMin;
Other than that, your method looks good to me.

BTW, loops are not evil; only eval() is evil.

Date Subject Author
1/24/13 Derek Goring
1/24/13 Bruno Luong