fast running mean

fast recersive running mean in 2D or 3D.
1.8K Downloads
Updated 10 Apr 2008

No License

[outmat] = fastrunmean(inmat,win,ptype);

fastrunmean computes the running mean of 'inmat' over a window of size 'win'. The argument 'ptype' controls the padding type, which decides how the edges are padded. An efficient recursive algorithm is used that is independent of the window size, so that the computational cost is only proportional to the size of the size input matrix.

Input
inmat - a 2D or 3D matrix
win - a matix of window lengths in each dimension. Windows should be odd in size!
ptype - a string describing how to treat edge padding
'zeros' - pad with zeros
'mean' - pad with the mean of inmat
note: plan to add more options when I have time.

Output
outmat - boxcar filtered inmat.

Much faster than SMOOTHN for boxcar filters and extends RUNMEAN to 2 and 3 dimensions.

Example and speed test

m2D = randn([600 300]);
m3D = randn([100 100 100]);

2D

d = fastrunmean(m2D,[51 51],'zeros');

3D

d = fastrunmean(m3D,[51 21 73],'zeros');

Speed test

for n=1:100
tic;d=fastrunmean(m2D,[51 51],'zeros');t(n)=toc;
end
fprinft('Averaged time per call over 100 calls = %d s',mean(t))

for n=1:100
tic;d=fastrunmean(m3D,[51 51 51],'zeros');t(n)=toc;
end
fprinft('Averaged time per call over 100 calls = %d s',mean(t))

You can alter the size of the windows to see that mean(t) is independent of the window size. The time is averaged to allow for fluctuations in computer performance. The average time will give you a feel for the speed.

Cite As

Neil Hodgson (2026). fast running mean (https://www.mathworks.com/matlabcentral/fileexchange/19504-fast-running-mean), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: smoothn

Version Published Release Notes
1.0.0.0

Added some examples. Changed the error checking a little.