Skip to main content
v2026.11,858 entries · CC-BY 4.0

MATLAB Moving Average: movmean, smoothdata and filter

Compute a MATLAB moving average with movmean, and see how smoothdata and filter differ on endpoints, NaN values and window alignment.

Written and maintained by CASRAI Editorial Board

Last updated

The MATLAB moving average is computed with movmean: M = movmean(A,k) returns the average over a sliding window of length k. For one-off smoothing of a signal, movmean is usually all you need. smoothdata uses the same average by default and offers other smoothers, and filter gives a trailing average whose first outputs include implied zeros. This page runs one 10-value vector through each so you can see how they differ. For the statistical idea itself, see what a moving average is.

What it is used for in research

A moving average replaces each value with the mean of its neighbours, which damps short-term noise so a slower trend is easier to see. Researchers use it to smooth instrument traces, time series and survey waves before plotting, or as a simple baseline before fitting a model such as the ones in ARIMA models for research time series. A weighted alternative is covered in the exponential smoothing formula. For signal background, see what is signal processing, and what MATLAB is used for places this function in the wider environment.

Install or access

movmean, smoothdata and filter are documented in the MATLAB reference on the MathWorks site: movmean, smoothdata and filter. Use the documentation for your own release as the authority on availability.

Step-by-step: smooth a vector with movmean

  1. Put your data in a vector A. This guide uses the example vector from the MathWorks movmean page: A = [4 8 6 -1 -2 -3 -1 3 4 5];
  2. Choose a window length k. Call M = movmean(A,3).
  3. Read the result: 6.0000 6.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000 4.5000.
  4. For a matrix, add a dimension argument: movmean(A,3,2) averages along the second dimension. By default the function works along the first non-singleton dimension.
  5. For timestamped data, pass sample points. With hourly timestamps t, movmean(A,hours(3),"SamplePoints",t) uses a time-based window; when the sample points are datetime or duration values, the window length must be a duration.

The full syntax list is movmean(A,k), movmean(A,[kb kf]), movmean(___,dim), movmean(___,nanflag) and movmean(___,Name,Value).

Centered versus trailing windows

For an odd k, the window is centered on the current element. For an even k, it is centered on the current and previous elements, so movmean(A,2) averages each value with the one before it (by that rule the first outputs are 4, 6, 7, 2.5). To control the window yourself, give [kb kf], the number of elements before and after the current one. A trailing window of the current and two previous values is movmean(A,[2 0]):

4.0000 6.0000 6.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000

Endpoints and NaN values

The default "Endpoints" setting is "shrink": near the edges the window shrinks to the elements that exist, which is why the first output above is 6, the mean of 4 and 8. Other options are "discard" (no output where the window does not fully overlap the data; not allowed for tables or timetables), "fill" (missing elements become NaN) and a numeric or logical scalar that replaces the missing elements. With movmean(A,3,"Endpoints","discard") the same vector returns 8 values:

6.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000

The default nanflag is "includemissing", so any NaN in a window makes that output NaN. With A = [4 8 NaN -1 -2 -3 NaN 3 4 5], movmean(A,3) returns 6 NaN NaN NaN -2 NaN NaN NaN 4 4.5, while movmean(A,3,"omitnan") returns 6 6 3.5 -1.5 -2 -2.5 0 3.5 4 4.5.

movmean, smoothdata or filter

Function What the documentation says Window alignment
movmean Moving average over a window; handles endpoints, NaN and sample points as above. Centered by default; [kb kf] sets it.
smoothdata(A,"movmean") “movmean” is the default method of smoothdata (same as smoothdata(A)); other methods include “movmedian”, “gaussian”, “sgolay” and “lowess”. Window form: smoothdata(A,"movmean",window). See the smoothdata page for window options.
filter Applies a rational transfer function; a moving average is the example b = (1/windowSize)*ones(1,windowSize); a = 1; y = filter(b,a,x);. Trailing: the current input and the previous windowSize-1 inputs.

The smoothdata page describes “movmedian” as the median over each window, the variant to try when outliers distort the mean, and “gaussian” as a Gaussian-weighted average over each window.

Output and how to read it

Compare the centered and trailing results on the example vector: the trailing movmean(A,[2 0]) is the centered movmean(A,3) shifted one position later, so a trailing window lags the data. Because filter is a trailing average and by default “initializes the filter delays as zero”, its first outputs treat the missing earlier inputs as zero. With windowSize = 3 on this vector the first three outputs are 1.3333, 4 and 6 (arithmetic: 4/3, 12/3, 18/3), and from the fourth output on they equal the trailing movmean(A,[2 0]) values (4.3333, 1, -2, …). Use movmean with its "Endpoints" option if you do not want those zero-padded starting values.

Troubleshooting

Why is my output full of NaN?

One NaN in the input contaminates every window that contains it under the default "includemissing". Pass "omitnan" to ignore them, as in the example above.

Why does the output have fewer elements than the input?

You used "Endpoints","discard", which drops outputs where the window does not fully overlap the data.

Why are the first values from filter too small?

The default filter delays are zero, so early windows include implied zeros. See the output section above.

Why is my smoothed curve shifted?

A trailing window ([kb kf] with kf = 0, or filter) lags the data. Use a centered window if alignment matters.

Error with a duration window

When "SamplePoints" is datetime or duration, the window length must be a duration, such as hours(3).

Free alternatives

Tool Function Documented behaviour
GNU Octave movmean(x, wlen) Same call shape as MATLAB, with [nb, na] windows, a dimension argument and a NaN condition. An even wlen takes wlen/2 elements on the left and wlen/2 - 1 on the right; the default "Endpoints" is "shrink".
Python pandas DataFrame.rolling(window, min_periods=None, center=False) followed by an aggregation such as .mean() Windows are trailing unless center=True; for an integer window, min_periods defaults to the window size, so the first rows are NaN rather than shrunk.

FAQ

How do I calculate a moving average in MATLAB?

Call movmean(A,k) with your data and the window length k. For a trailing window of length k use movmean(A,[k-1 0]).

Is movmean the same as smoothdata?

For the default case, yes: “movmean” is the default method of smoothdata, which also offers movmedian, gaussian, sgolay and lowess.

Can I use filter for a moving average?

Yes, with b = (1/windowSize)*ones(1,windowSize) and a = 1; expect zero-initialized delays at the start.

How do I ignore NaN values?

Add "omitnan": movmean(A,3,"omitnan").

Sources

Official documentation, with the date each page was checked.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB Moving Average: movmean, smoothdata and filter

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.