movprod
Moving product
Syntax
Description
M = movprod( returns the
local A,k)k-point products, where each product is calculated over a
sliding window of length k across neighboring elements of
A. M is the same size as
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 the current and previous elements.
The window size is automatically truncated at the endpoints when there
are not enough elements to fill the window. When the window is truncated,
the product is taken over only the elements that fill the window.
If
Ais a vector, thenmovprodoperates along the length of the vectorA.If
Ais a multidimensional array, thenmovprodoperates along the first dimension ofAwhose size does not equal 1.If
Ais a table or timetable, thenmovprodoperates along the variables ofA. (since R2025a)
M = movprod(___, specifies the
dimension of dim)A to operate along for any of the previous syntaxes.
For example, if A is a matrix, then
movprod(A,k,2) operates across the columns of
A, computing the k-element sliding product
for each row.
M = movprod(___, specifies
whether to include or omit nanflag)NaN values in A.
For example, movprod(A,k,"omitnan") ignores
NaN values when computing each product. By default,
movprod includes NaN values.
M = movprod(___, specifies
additional parameters for the moving product using one or more name-value pair
arguments. For example, if Name,Value)x is a vector of time values, then
movprod(A,k,"SamplePoints",x) computes the moving product
relative to the times in x.
Examples
Compute the three-point centered moving product of a row vector. When there are fewer than three elements in the window at the endpoints, take the product over the elements that are available.
A = [1 2 4 -1 -2 -3 -1 3 2 1]; M = movprod(A,3)
M = 1×10
2 8 -8 8 -6 -6 9 -6 6 2
Compute the three-point trailing moving product of a row vector. When there are fewer than three elements in the window at the endpoints, take the product over the elements that are available.
A = [1 2 1 -1 -2 -3 -1 3 4 1]; M = movprod(A,[2 0])
M = 1×10
1 2 2 -2 2 -6 -6 9 -12 12
Compute the 3-point centered moving product for each row of a matrix. The dimension argument is 2, which slides the window across the columns of A. The window starts on the first row, slides horizontally to the end of the row, then moves to the second row, and so on.
A = [1 2 1; -1 -2 -3; -1 3 4]
A = 3×3
1 2 1
-1 -2 -3
-1 3 4
M = movprod(A,3,2)
M = 3×3
2 2 2
2 -6 6
-3 -12 12
Create a row vector containing NaN values.
A = [4 8 NaN -1 -2 -3 NaN 3 4 5];
Compute the three-point centered moving product of the vector, excluding NaN values. For windows that contain any NaN value, movprod computes with the non-NaN elements.
M = movprod(A,3,"omitnan")M = 1×10
32 32 -8 2 -6 6 -9 12 60 20
Compute a 3-hour centered moving product 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 = 1×6 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 = movprod(A,k,"SamplePoints",t)M = 1×6
32 192 -48 12 -6 6
Compute the three-point centered moving product of a row vector, but discard any calculation that uses fewer than three points from the output. In other words, return only the products computed from a full three-element window, discarding endpoint calculations.
A = [1 2 1 -1 -2 -3 -1 3 4 1]; M = movprod(A,3,"Endpoints","discard")
M = 1×8
2 -2 2 -6 -6 9 -12 12
Create a table from the columns of a magic square.
A = magic(3); T = array2table(A)
T=3×3 table
A1 A2 A3
__ __ __
8 1 6
3 5 7
4 9 2
Calculate the moving products of the table variables using movprod.
M = movprod(T,3)
M=3×3 table
A1 A2 A3
__ __ __
24 5 42
96 45 84
12 45 14
To calculate moving products only for specified variables, specify the DataVariables name-value argument.
M2 = movprod(T,3,DataVariables=["A2" "A3"])
M2=3×3 table
A1 A2 A3
__ __ __
8 5 42
3 45 84
4 45 14
To append the moving products to the table instead of replacing values, specify ReplaceValues as false.
M3 = movprod(T,3,ReplaceValues=false)
M3=3×6 table
A1 A2 A3 A1_movprod A2_movprod A3_movprod
__ __ __ __________ __________ __________
8 1 6 24 5 42
3 5 7 96 45 84
4 9 2 12 45 14
Input Arguments
Input data, specified as a vector, matrix, multidimensional array, table, or timetable.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | table | timetable
Window length, specified as a numeric or duration scalar. When k is a
positive integer scalar, the centered product includes the element in the
current position plus surrounding neighbors.
For example, movprod(A,3) computes an array of local
three-point products.

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, movprod(A,[2 1]) computes an array of
local four-point products.
![movprod(A,[2 1]) computation. The elements in the sample window are 4, 1, 3, and 2, so the resulting local product is 24.](movprod_windowing.png)
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 whose size does not equal 1.
Consider an m-by-n input matrix,
A:
movprod(A,k,1)computes thek-element sliding product for each column ofAand returns anm-by-nmatrix.
movprod(A,k,2)computes thek-element sliding product for each row ofAand returns anm-by-nmatrix.
If A
is a table or timetable, then you cannot specify dim. The
movprod function always operates along the variables of tables and
timetables. (since R2025a)
Missing value condition, specified as one of these values:
"includemissing"or"includenan"— IncludeNaNvalues inAwhen computing each product. If any element in the window isNaN, then the corresponding element inMisNaN."includemissing"and"includenan"have the same behavior."omitmissing"or"omitnan"— IgnoreNaNvalues inA, and compute each product over fewer points. If all elements in the window areNaN, then the corresponding element inMis 1."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 = movprod(A,k,"Endpoints","fill")
Method to treat windows near endpoints, specified as one of these options:
| Value | Description |
|---|---|
"shrink" | Shrink the window size near the endpoints of the input to include only existing elements. |
"discard" | Do not output any product values when the window does not completely overlap with existing elements. If |
"fill" | Replace nonexisting elements with
NaN. |
| numeric or logical scalar | Replace nonexisting elements with the specified numeric or logical value. |
Sample points for computing products, 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 movprod(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".
Since R2025a
Table or timetable variables to operate on, specified as one of the options in this table.
If you do not specify
DataVariables, thenmovprodoperates on all variables. This behavior is the default behavior.If you specify
DataVariables, thenmovprodoperates only on the specified variables. Other variables not specified byDataVariablespass through to the output without modification.
| Indexing Scheme | Values to Specify | Examples |
|---|---|---|
Variable name |
|
|
Variable index |
|
|
Function handle |
|
|
Variable type |
|
|
Since R2025a
Replace values indicator, specified as one of these values when A
is a table or timetable.
trueor1— In the output table or timetable, replace input table or timetable variables with variables containing output values frommovprod.falseor0— Append variables containing output values frommovprodto the output table or timetable.
For vector, matrix, or multidimensional array input data,
ReplaceValues is not supported.
Extended Capabilities
This function supports tall arrays with the limitations:
The
SamplePoints,DataVariables, andReplaceValuesname-value arguments are not supported.Table and timetable inputs are not supported.
For more information, see Tall Arrays.
Usage notes and limitations:
The
DataVariablesandReplaceValuesname-value arguments are not supported.Table and timetable inputs are not supported.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
The movprod function
supports GPU array input with these usage notes and limitations:
The
SamplePoints,DataVariables, andReplaceValuesname-value arguments are not supported.Table and timetable inputs are not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Usage notes and limitations:
The
DataVariablesandReplaceValuesname-value arguments are not supported.Table and timetable inputs are not supported.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2017aYou can specify tables and timetables as inputs containing numeric data to operate on. By
default, movprod operates on all variables in a table or timetable.
However, you can specify which variables to operate on. You can also append output values
from movprod as new variables in the output table or
timetable.
Include or omit missing values in the input array when computing each product by
using the "includemissing" or "omitmissing"
options. These options have the same behavior as the "includenan"
and "omitnan" options, respectively.
The movprod function shows improved performance when
computing over a matrix when there are sample points.
For example, this code computes the moving product of a 300-by-300 matrix with corresponding sample points. The code is about 2.8x faster than in the previous release.
function timingMovprod A = randn(300); t = sort(rand(300,1)); tic for k = 1:2000 movprod(A,0.1,"SamplePoints",t); end toc end
The approximate execution times are:
R2022b: 0.92 s
R2023a: 0.33
The code was timed on a Windows® 10, Intel®
Xeon® CPU E5-1650 v4 @ 3.60 GHz test system by calling the
timingMovprod function.
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.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- 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
- United Kingdom (English)