fast running mean
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
Platform Compatibility
Windows macOS LinuxCategories
- Image Processing and Computer Vision > Computer Vision Toolbox > Recognition, Object Detection, and Semantic Segmentation > Image Category Classification >
Tags
Acknowledgements
Inspired by: smoothn
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
| Version | Published | Release Notes | |
|---|---|---|---|
| 1.0.0.0 | Added some examples. Changed the error checking a little. |
