File Exchange

## smooth2a

version 1.0.0.0 (1.27 KB) by Greg Reeves

### Greg Reeves (view profile)

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

Updated 13 Mar 2009

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.

Henry Wu

yanqin Li

### Mohammad Amin Kazemi (view profile)

A very useful function. Thank you.

In the next versions, It would be great if you could add an option that the function does not change the arrays having a specific value. For example, if a cell has got the value of 0, smoothing does not change the value of that cell. It is really useful in image processing by PIV/PTV where the users need to impose a zero velocity at the solid walls and smooth the rest of the domain.

Thank you anyway for the function and hope to see it developed further in the future.

Kai-Chih Tseng

Kai-Chih Tseng

AJAY RAGHAVENDRA

Rock Zhao

Shan Dou

Arthur PINON

perfect

Merrill Yeung

### Merrill Yeung (view profile)

Do you have the 1D version?

JIANG LAO

### JIANG LAO (view profile)

thanks very much!

Poonperm Vardhanabindu

### Poonperm Vardhanabindu (view profile)

Simple and effective

can zu

### can zu (view profile)

is there weights in the function?

Jeremy Parker

### Jeremy Parker (view profile)

Works just as advertised, thank you!!

Ahmed Hussein

Xiaolin

### Xiaolin (view profile)

very good code for 2-D data smoothing

Enrico Salvati

William Yoshida

### William Yoshida (view profile)

I had been searching for something that works like this for some time. Exactly what I had hoped for. Thank you!

Beichuan Hong

### Beichuan Hong (view profile)

The function works completely as advertised.

David MERCIER

Liem Nguyen

### Liem Nguyen (view profile)

It works perfectly. Thank you!

Peter

### Peter (view profile)

The function works completely as advertised. Thank you.

In response to JEC PsiCom's comment from 2011 (since I stumbled across the same error)... Make sure your input matrix is of type "double". My problem was solved by casting the input as a double {eg, xs = smooth2a(double(x),1);}

Kiwi

Stephanie

JEC PsiCom

### JEC PsiCom (view profile)

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?

Nikola Toljic

Thanks.

Mark Novak

### Mark Novak (view profile)

Thank you... this is perfectly implemented.

Jakob

### Jakob (view profile)

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!

Wuxing Gan

### Wuxing Gan (view profile)

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

Nathan

### Nathan (view profile)

Very slick code. Runs fast. Works Great!

### Carlos Adrian Vargas Aguilera (view profile)

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

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