5.0

5.0 | 6 ratings Rate this file 44 Downloads (last 30 days) File Size: 2.53 KB File ID: #23287
image thumbnail

smooth2a

by Greg Reeves

 

13 Mar 2009

Smooths a 2D matrix using a mean filter over a user-defined rectangle. Ignores and preserves NaNs.

| Watch this File

File Information
Description

Smooths 2D array data. Ignores NaN's.

function matrixOut = smooth2a(matrixIn,Nr,Nc)
 
This function smooths the data in matrixIn using a mean filter over a
rectangle of size (2*Nr+1)-by-(2*Nc+1). Basically, you end up replacing
element "i" by the mean of the rectange centered on "i". Any NaN
elements are ignored in the averaging. If element "i" is a NaN, then it
will be preserved as NaN in the output. At the edges of the matrix,
where you cannot build a full rectangle, as much of the rectangle that
fits on your matrix is used (similar to the default on Matlab's builtin
function "smooth").

"matrixIn": original matrix
"Nr": number of points used to smooth rows
"Nc": number of points to smooth columns. If not specified, Nc = Nr.

"matrixOut": smoothed version of original matrix

%%%%%%%%%

Explanation of screenshot:
The left plot is a pcolor (shading interp, axis equal) of a "jagged" 906x1024 matrix. Note that the edges have NaN values (white coloring). On the right, pcolor of output of smooth2a, with Nr = Nc = 50. NaN's are preserved, but do not affect the smoothing of adjacent elements.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
smooth2

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
17 Mar 2009 Adriano Azzarone  
17 Mar 2009 Adriano Azzarone

This function is what I have searched for months.
It's working great!!
My best congrats

20 Mar 2009 Carlos Adrian Vargas Aguilera

Nice code, my codes moving_average.m and ndnanfilter.m do similar jobs but are written very different! Regards...

21 Jun 2010 Nathan

Very slick code. Runs fast. Works Great!

17 Aug 2010 Wuxing Gan

very useful, I specially like its way to handle the NaN points.
thanks.

23 Aug 2010 Jakob

I experienced vertical and horizontal jumps when using the smooth2 procedure and spent quite a while tracking down something better when I stumbled across the list of work the smooth2 had inspired (yours being one of them). smooth2a completely resolved the issue. I thank you for this!

07 Dec 2010 Mark Novak

Thank you... this is perfectly implemented.

02 Jan 2011 Nikola Toljic

Thanks.

24 Jan 2011 JEC PsiCom

I get this error trying to run the code on a 2-D matrix of type double:

"MTIMES is not supported for one sparse input and one single input."

What does this mean?

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
2d Greg Reeves 13 Mar 2009 16:18:34
mathematics Greg Reeves 13 Mar 2009 16:18:36
interpolation Greg Reeves 13 Mar 2009 16:18:36
approximation Greg Reeves 13 Mar 2009 16:18:36
matrix Greg Reeves 13 Mar 2009 16:18:36
smooth Greg Reeves 13 Mar 2009 16:18:36
smoothing Greg Reeves 13 Mar 2009 16:18:36

Contact us at files@mathworks.com