Moving mean - MATLAB movmean (2024)

Table of Contents
Syntax Description Examples Centered Moving Average of Vector Trailing Moving Average of Vector Moving Average of Matrix Moving Average of Vector with Missing Values Sample Points for Moving Average Return Only Full-Window Averages Input Arguments A — Input array vector | matrix | multidimensional array k — Window length numeric or duration scalar [kb kf] — Directional window length numeric or duration row vector containing two elements dim — Dimension to operate along positive integer scalar nanflag — Missing value condition "includemissing" (default) | "includenan" | "omitmissing" | "omitnan" Name-Value Arguments Endpoints — Method to treat windows near endpoints "shrink" (default) | "discard" | "fill" | numeric or logical scalar SamplePoints — Sample points for computing averages vector More About Mean Extended Capabilities Tall Arrays Calculate with arrays that have more rows than fit in memory. C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™. Thread-Based EnvironmentRun code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool. GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™. Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™. Version History R2023a: Specify missing value condition R2023a: Improved performance when computing over matrix with sample points See Also Functions Topics MATLAB Command Americas Europe Asia Pacific References

Moving mean

collapse all in page

Syntax

M = movmean(A,k)

M = movmean(A,[kb kf])

M = movmean(___,dim)

M = movmean(___,nanflag)

M = movmean(___,Name,Value)

Description

example

M = movmean(A,k) returnsan array of local k-point mean values, where each mean is calculated overa sliding window of length k across neighboringelements of A. When k is odd,the window is centered about the element in the current position.When k is even, the window is centered about thecurrent and previous elements. The window size is automatically truncatedat the endpoints when there are not enough elements to fill the window.When the window is truncated, the average is taken over only the elementsthat fill the window. M is the same size as A.

  • If A is a vector, then movmean operates along the length of the vector A.

  • If A is a multidimensional array, then movmean operates along the first dimension of A whose size does not equal 1.

example

M = movmean(A,[kb kf]) computesthe mean with a window of length kb+kf+1 that includesthe element in the current position, kb elementsbackward, and kf elements forward.

example

M = movmean(___,dim) specifies the dimension of A to operate along for any of the previous syntaxes. For example, if A is a matrix, then movmean(A,k,2) operates along the columns of A, computing the k-element sliding mean for each row.

example

M = movmean(___,nanflag) specifies whether to include or omit NaN values in A. For example, movmean(A,k,"omitnan") ignores NaN values when computing each mean. By default, movmean includes NaN values.

example

M = movmean(___,Name,Value) specifies additional parameters for the moving average using one or more name-value arguments. For example, if x is a vector of time values, then movmean(A,k,"SamplePoints",x) computes the moving average relative to the times in x.

Examples

collapse all

Centered Moving Average of Vector

Open Live Script

Compute the three-point centered moving average of a row vector. When there are fewer than three elements in the window at the endpoints, take the average over the elements that are available.

A = [4 8 6 -1 -2 -3 -1 3 4 5];M = movmean(A,3)
M = 1×10 6.0000 6.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000 4.5000

Trailing Moving Average of Vector

Open Live Script

Compute the three-point trailing moving average of a row vector. When there are fewer than three elements in the window at the endpoints, take the average over the elements that are available.

A = [4 8 6 -1 -2 -3 -1 3 4 5];M = movmean(A,[2 0])
M = 1×10 4.0000 6.0000 6.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000

Moving Average of Matrix

Open Live Script

Compute the three-point centered moving average for each row of a matrix. The window starts on the first row, slides horizontally to the end of the row, then moves to the second row, and so on. The dimension argument is two, which slides the window across the columns of A.

A = [4 8 6; -1 -2 -3; -1 3 4]
A = 3×3 4 8 6 -1 -2 -3 -1 3 4
M = movmean(A,3,2)
M = 3×3 6.0000 6.0000 7.0000 -1.5000 -2.0000 -2.5000 1.0000 2.0000 3.5000

Moving Average of Vector with Missing Values

Open Live Script

Compute the three-point centered moving average of a row vector containing two NaN elements.

A = [4 8 NaN -1 -2 -3 NaN 3 4 5];M = movmean(A,3)
M = 1×10 6.0000 NaN NaN NaN -2.0000 NaN NaN NaN 4.0000 4.5000

Recalculate the average, but omit the NaN values. When movmean omits NaN elements, it computes the average over the remaining elements in the window.

M = movmean(A,3,"omitnan")
M = 1×10 6.0000 6.0000 3.5000 -1.5000 -2.0000 -2.5000 0 3.5000 4.0000 4.5000

Sample Points for Moving Average

Open Live Script

Compute a 3-hour centered moving average of the data in A according to the time vector t.

A = [4 8 6 -1 -2 -3];k = hours(3);t = datetime(2016,1,1,0,0,0) + hours(0:5)
t = 1x6 datetime 01-Jan-2016 00:00:00 01-Jan-2016 01:00:00 01-Jan-2016 02:00:00 01-Jan-2016 03:00:00 01-Jan-2016 04:00:00 01-Jan-2016 05:00:00
M = movmean(A,k,"SamplePoints",t)
M = 1×6 6.0000 6.0000 4.3333 1.0000 -2.0000 -2.5000

Return Only Full-Window Averages

Open Live Script

Compute the three-point centered moving average of a row vector, but discard any calculation that uses fewer than three points from the output. In other words, return only the averages computed from a full three-element window, discarding endpoint calculations.

A = [4 8 6 -1 -2 -3 -1 3 4 5];M = movmean(A,3,"Endpoints","discard")
M = 1×8 6.0000 4.3333 1.0000 -2.0000 -2.0000 -0.3333 2.0000 4.0000

Input Arguments

collapse all

AInput array
vector | matrix | multidimensional array

Input array, specified as a vector, matrix, or multidimensionalarray.

kWindow length
numeric or duration scalar

Window length, specified as a numeric or duration scalar. When k is a positive integer scalar, the centered average includes the element in the current position plus surrounding neighbors.

For example, movmean(A,3) computes an array of local three-point mean values.

Moving mean - MATLAB movmean (1)

[kb kf]Directional window length
numeric or duration row vector containing two elements

Directional window length, specified as a numeric or duration row vector containing two elements. When kb and kf are positive integer scalars, the calculation is over kb+kf+1 elements. The calculation includes the element in the current position, kb elements before the current position, and kf elements after the current position.

For example, movmean(A,[2 1]) computes an array of local four-point mean values.

Moving mean - MATLAB movmean (2)

dimDimension to operate along
positive integer scalar

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.

Dimension dim indicates the dimension that movmean operatesalong, that is, the direction in which the specified window slides.

Consider an m-by-n input matrix, A:

  • movmean(A,k,1) computes the k-element sliding mean for each column of A and returns an m-by-n matrix.

    Moving mean - MATLAB movmean (3)

  • movmean(A,k,2) computes the k-element sliding mean for each row of A and returns an m-by-n matrix.

    Moving mean - MATLAB movmean (4)

nanflagMissing value condition
"includemissing" (default) | "includenan" | "omitmissing" | "omitnan"

Missing value condition, specified as one of these values:

  • "includemissing" or "includenan" — Include NaN values in A when computing each mean. If any element in the window is NaN, then the corresponding element in M is NaN. "includemissing" and "includenan" have the same behavior.

  • "omitmissing" or "omitnan" — Ignore NaN values in A and compute each mean over fewer points. If all elements in the window are NaN, then the corresponding element in M is NaN. "omitmissing" and "omitnan" have the same behavior.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: M = movmean(A,k,"Endpoints","fill")

EndpointsMethod to treat windows near endpoints
"shrink" (default) | "discard" | "fill" | numeric or logical scalar

Method to treat windows near endpoints, specified as one of these options:

ValueDescription
"shrink"Shrink the window size near the endpoints of the input to include only existing elements.
"discard"Do not output any average values when the window does not completely overlap with existing elements.
"fill"Replace nonexisting elements with NaN.
numeric or logical scalarReplace nonexisting elements with the specified numeric or logical value.

SamplePointsSample points for computing averages
vector

Sample points for computing averages, specified as a vector. The sample points represent the locations of the data in A. Sample points do not need to be uniformly sampled. By default, the sample points vector is [1 2 3 ... ].

Moving windows are defined relative to the sample points, which must be sorted and contain unique elements. For example, if t is a vector of times corresponding to the input data, then movmean(rand(1,10),3,"SamplePoints",t) has a window that represents the time interval between t(i)-1.5 and t(i)+1.5.

When the sample points vector has data type datetime or duration, then the moving window length must have type duration.

If the sample points are nonuniformly spaced and Endpoints is specified, then its value must be "shrink".

More About

collapse all

Mean

For a finite-length vector A made up of N scalar observations, the mean is defined as

μ=1Ni=1NAi.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2016a

expand all

Include or omit missing values in the input array when computing each mean by using the "includemissing" or "omitmissing" options. These options have the same behavior as the "includenan" and "omitnan" options, respectively.

See Also

Functions

  • movmedian | movvar | movstd | movsum | movmad | mean | smoothdata

Topics

  • Data Smoothing and Outlier Detection

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Moving mean - MATLAB movmean (5)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Moving mean - MATLAB movmean (2024)

References

Top Articles
If A Thermometer Indicates 40 Degrees Celsius
Yuki Mago Profile, Age, Birthday, Height - Kpop Singers
Woman Jumps Off Mount Hope Bridge 2022
Registrar Utd
Matka 786 Guessing
Craigslist Rooms For Rent North County San Diego
Fkiqx Breakpoints
Discovering The Height Of Hannah Waddingham: A Look At The Talented Actress
Cookie Clicker The Advanced Method
Craigslist Shelves
Apple Store Near Me Make Appointment
Wolfgang's Thanks Crossword
Stepmom Full Video Hd
The First 10 Years, Leslie Bricusse - Qobuz
Texas Motors Specialty Photos
9:00 A.m. Cdt
American Eagle Store Locator
Free Bubble Letters Generator | Add bubble letters with a click!
Food King El Paso Ads
Adopting Remote UniFi Devices with Windows Server DHCP – itramblings
Rubmaps Springfield
Lucio Surf Code
No Prob-Llama Plotting Points
Rural King Credit Card Minimum Credit Score
Vidant My Chart Login
Jockey Standings Saratoga 2023
Virtualrewardcenter.com/Activate
ONE PAN BROCCOLI CASHEW CHICKEN
Rugged Gentleman Barber Shop Martinsburg Wv
Lux Nails Columbia Mo
Tackytwinzzbkup
Reely Hooked Fish Dip Amazon
Rugrats in Paris: The Movie | Rotten Tomatoes
Rare Rides: The 1970 Chevrolet Chevelle SS454 LS6 Convertible - Street Muscle Rare Rides
9044906381
Dki Brain Teaser
Fx Channel On Optimum
Best Turntables of 2023 - Futurism
Seats 3D Ubs Arena
Morning Call Obits Today Legacy
Chars Boudoir
Omari Lateef Mccree
600 Aviator Court Vandalia Oh 45377
Pre-Order Apple Watch Series 10 – Best Prices in Dubai, UAE
About My Father Showtimes Near Marcus Saukville Cinema
Watch Races - Woodbine Racetrack
Assistant Store Manager Dollar General Salary
Sesame Street 4323
Senna Build Guides :: League of Legends Strategy Builds, Runes, Items, and Abilities :: Patch 14.18
German police arrest 25 suspects in plot to overthrow state – DW – 12/07/2022
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6489

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.