File Exchange

image thumbnail


version 2.3 (9.47 KB) by Damien Garcia
SMOOTHN allows automatized and robust smoothing in arbitrary dimension w/wo missing values


Updated 20 Jun 2020

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

SMOOTHN provides a fast, unsupervised and robust discretized spline smoother for data of arbitrary dimension.

SMOOTHN(Y) automatically smoothes the uniformly-sampled array Y. Y can be any N-D multicomponent noisy array (e.g. time series, images, 3D data, 3D vector fields, tensors...).

To smooth a vector field or multi-component data, Y must be a cell array. For example, if you need to smooth a 3-D vectorial flow (Vx,Vy,Vz), use Y = {Vx,Vy,Vz}. The output Z is also a cell array which contains the smoothed components.

SMOOTHN can deal with missing (NaN) values (see screenshot and examples).

SMOOTHN(...,'robust') carries out a robust smoothing that minimizes the influence of outlying data (see screenshot and examples).

SMOOTHN is made unsupervised by the minimization of the generalized cross-validation score.

Enter "help smoothn" in the Matlab command window for complete instructions and 1-D to 3-D examples.

A series of 8 documented examples is available here:

When using this algorithm, please refer to these 2 papers:
1) Garcia D. Robust smoothing of gridded data in one and higher dimensions with missing values.
Comput Statist Data Anal, 2010;54:1167-1178
2) Garcia D. A fast all-in-one method for automated post-processing of PIV data.
Exp Fluids, 2011;50:1247-1259.

Cite As

Damien Garcia (2021). smoothn (, MATLAB Central File Exchange. Retrieved .

Garcia, Damien. “Robust Smoothing of Gridded Data in One and Higher Dimensions with Missing Values.” Computational Statistics & Data Analysis, vol. 54, no. 4, Elsevier BV, Apr. 2010, pp. 1167–78, doi:10.1016/j.csda.2009.09.020.

View more styles

Garcia, Damien. “A Fast All-in-One Method for Automated Post-Processing of PIV Data.” Experiments in Fluids, vol. 50, no. 5, Springer Science and Business Media LLC, Oct. 2010, pp. 1247–59, doi:10.1007/s00348-010-0985-y.

View more styles

Comments and Ratings (57)

hongdou gao

When I enter "Z = smoothn(y)" in the matlab command line, the following error will appear: "lambda = lambda + (2-2 * cos (pi * (reshape (1: sizy (I), siz0) - 1) / sizy (I))) / di (I) ^ 2;(line 384)" and prompt "wrong use + matrix dimension must be consistent". Why and what is the solution,Thanks a lot!

Philip G

Very useful function. Thank you.
NOTE: The function might not work if you compile it! It uses the function "license" to check if image toolbox is installed. This function will always be true for compiled programs. So if you compile this without having the image toolbox (with bwdist function inparticular) - you need to change the license check in the code manually to always use the alternative method.

Uwe Ehret

Thank you for this great tool!

Taindra Neupane

This function with robust works most of the time but with default, it gives like this Warning: S = 9.655e-04: the lower bound for S has been reached. Specify S as an input variable.
> In smoothn (line 509)
Maybe it needs to be fixed.


Thank you!

Nishant Kumar

For resolving the error: 'Error using .* : Matrix dimensions must agree.' when smoothening a surface, replace
y = y.*w{dim};
in smoothn>dctn and smoothn>idctn with
y = y.*w{dim}(:,ones(1,n));


job rosier

I get an error message when I try to run you code with Example #7: smooth a 2-D vector field:

Error using .*
Matrix dimensions must agree.

Error in smoothn>dctn (line 728)
y = y.*w{dim};

Is this due to your latest update?
I'm using matlab version 2016a.


I want to give this a good rating, but I an told I need to download DCTN and IDCTN. The link given to be doesn't lead anywhere. If something is required to use the function, why wouldn't it be included in the download?



shaojiang wu

Abhinav Gaur

Bochen ZHANG



Very nice. Many thanks

Shalin Parikh

Hi Damien, Is there any minimum and maximum value for the smoothing parameter S used?

Parker Sorenson

Dang Le Van

Just used a part of the code. Very clear instruction. Well done! Thanks for sharing.

Dipankar Kumar

Thanks to Damien Garcia for your robust and fast function for smooting.

Sathish Sanjeevi

This function is gold! Would be largely helpful for the scientific community across different disciplines.

Michal Kvasnicka

Perfect ... !!! Robust and fast function for smoothing.


works great and very smoothly :)

Ned Gulley

I featured this file in a blog post about how to write good help for your function. Thanks and well done!

Brian McFall

Works great and it's easy to adjust the robustness of the smoothing function.

Kevin Miller

Stephan Koehler

I'm trying to run this on large data sets, which crashes my computer. Is there any way to chunk it, i.e. break up say a 1000x1000x1000 array into pieces and merge the smoothened pieces?




Hi Damien

Im applying smoothn in order to smooth bathmetric data sets and it works perfectly. I'm using a smooth factor of 10^9 on a 1601X1601 grid. The spacing of the points in realty is 12.5 m. can you please tell me what is the spatial meaning of the smooth factor in my case ?



Rogier Westerhoff

Works fine for me!

John Booker

Matteo Niccoli

Jospeh Chang


hey Damien,

is there a way to suppress filling of missing values (nans to be specific)? it'd be really useful if this were an option.



francisco fuentes

Really nice tool, thank you.


Hi Damien,
What if I have non-uniformly sampled array? Could it be possible to us this function? Currently, I found the smoothed curve did not maintain the start and end points in 3D. The smoothed curve looks being shrunk.

Martin Lechner

Super tool for smoothing!


Thanks for sharing!! This is really a nice pice of code.


What is with the "DCTN and iDCTN are required"? I was able to access them outside the MatLab browser, but not from within it.


Jakub Wosyka

Hello Damien,
very useful and nice function.
I tried to add the non-uniform grid (x values) according to your article but then it only interpolates the data. When I raise the value of smooting parameter, new the function will fall to zero (zero mean). (...probably my fault...). Do you plan to extend this nice function for non-uniform grids? Thank you

Erdal Bizkevelci


Really nice method!

I does seem to me though that there is a bug when using this in combination with your pppiv-function.

In the function RobustWeights, there is a line:
MAD = median(abs(r(I)-median(r(I))));

However, the values of r are complex when using it together with pppiv. That causes the median function to sort the values by the real component and then take the middle value. I think you probably want to add
r = abs(r);
before taking the median to sort the residuals by magnitude instead. Is that correct?


Well done! well documented. very easy to use.


I like this function very much...I do have a question though. I am smoothing a 50x50x3 n-d array but after smoothing my 3-d object is much flatter than the original. it almost seems distorted...has this ever happened to you before?


Very good!. Minor effort to obtain dctn and idctn but this is an easy way to smooth out "zigzag" contours (much better than going with INTERP2( Array, ntimes)).
thank you.

Damien Garcia

to Matthew: you should have read the File description:
MATLAB release: MATLAB 7.5 (R2007b)
BSXFUN is nonexistent in versions prior to 7.4!
Regards. DG

Matthew Souders

Code does not work out of the box for MATLAB 7.1 - at least I was unable to get it to function. Attempted a test case and got this error:

??? Undefined function or method 'bsxfun' for input argument of type 'function_handle'.

Error in ==> smoothn at line 266
Lambda = bsxfun(@plus,Lambda...

If some special set-up is required to make this code run successfully, this needs to be carefully documented.

Mark Shore

Mark Shore

Definitely very interesting looking. Well referenced with a range of 1, 2 and 3D examples, which work quite well. I'll rate this once I've had a chance to look at it in more detail.

However the author should correct the required products listing since the image processing toolbox is NOT a requirement per se. All of the provided test examples work perfectly without it. A call to bwdist to replace NaNs in supplied data defaults gracefully to another option if the image processing toolbox is not installed. And rgb2gray and imnoise are required only to _create_ the example of the noisy Saturn image given above, not to denoise it.

It would also be convenient for the author to include his subroutines dctn.m and idctn.m in the FEX file if possible, rather than requiring a separate download from an external web site.

Rob Campbell

This is nice!


H1 line, really descriptive help, examples, date and author mentioned, comments in the code, referenced method, and finally it works well.


Good job!

Welcome back!

MATLAB Release Compatibility
Created with R2017a
Compatible with R2017a to R2020a
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!