# movvar

Moving variance

## Syntax

``M = movvar(A,k)``
``M = movvar(A,[kb kf])``
``M = movvar(___,w)``
``M = movvar(___,w,dim)``
``M = movvar(___,nanflag)``
``M = movvar(___,Name,Value)``

## Description

example

````M = movvar(A,k)` returns an array of local `k`-point variance values, where each variance is calculated over a sliding window of length `k` across neighboring elements 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 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 variance is taken over only the elements that fill the window. `M` is the same size as `A`.If `A` is a vector, then `movvar` operates along the length of the vector `A`.If `A` is a multidimensional array, then `movvar` operates along the first dimension of `A` whose size does not equal 1.```

example

````M = movvar(A,[kb kf])` computes the variance with a window of length `kb+kf+1` that includes the element in the current position, `kb` elements backward, and `kf` elements forward.```

example

````M = movvar(___,w)` specifies a normalization factor for any of the previous syntaxes. When ```w = 0``` (default), `M` is normalized by `k-1` for window length `k`. When `w = 1`, `M` is normalized by `k`.```

example

````M = movvar(___,w,dim)` specifies the dimension of `A` to operate along for any of the previous syntaxes. Always specify the weight `w` from the previous syntax when specifying `dim`. For example, if `A` is a matrix, then `movvar(A,k,0,2)` operates along the columns of `A`, computing the `k`-element sliding variance for each row. The normalization factor is the default, `k-1`.```

example

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

example

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

## Examples

collapse all

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

```A = [4 8 6 -1 -2 -3 -1 3 4 5]; M = movvar(A,3)```
```M = 1×10 8.0000 4.0000 22.3333 19.0000 1.0000 1.0000 9.3333 7.0000 1.0000 0.5000 ```

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

```A = [4 8 6 -1 -2 -3 -1 3 4 5]; M = movvar(A,[2 0])```
```M = 1×10 0 8.0000 4.0000 22.3333 19.0000 1.0000 1.0000 9.3333 7.0000 1.0000 ```

Compute the three-point centered moving variance of a row vector and normalize each variance by the number of elements in the window.

```A = [4 8 6 -1 -2 -3 -1 3 4 5]; M = movvar(A,3,1)```
```M = 1×10 4.0000 2.6667 14.8889 12.6667 0.6667 0.6667 6.2222 4.6667 0.6667 0.2500 ```

Compute the three-point centered moving variance 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`. Always specify the normalization factor when specifying the dimension.

```A = [4 8 6; -1 -2 -3; -1 3 4]; M = movvar(A,3,0,2)```
```M = 3×3 8.0000 4.0000 2.0000 0.5000 1.0000 0.5000 8.0000 7.0000 0.5000 ```

Create a row vector containing `NaN` values.

`A = [4 8 NaN -1 -2 -3 NaN 3 4 5];`

Compute the three-point centered moving variance of the vector, excluding `NaN` values. For windows that contain any `NaN` value, `movvar` computes with the non-`NaN` elements.

`M = movvar(A,3,"omitnan")`
```M = 1×10 8.0000 8.0000 40.5000 0.5000 1.0000 0.5000 18.0000 0.5000 1.0000 0.5000 ```

Compute a 3-hour centered moving variance 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 = movvar(A,k,"SamplePoints",t)`
```M = 1×6 8.0000 4.0000 22.3333 19.0000 1.0000 0.5000 ```

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

```A = [4 8 6 -1 -2 -3 -1 3 4 5]; M = movvar(A,3,"Endpoints","discard")```
```M = 1×8 4.0000 22.3333 19.0000 1.0000 1.0000 9.3333 7.0000 1.0000 ```

## Input Arguments

collapse all

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

Data Types: `single` | `double` | `logical`

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

For example, `movvar(A,3)` computes an array of local three-point variances.

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, `movvar(A,[2 1])` computes an array of local four-point variances.

Weight, specified as one of these values:

• `0` — Normalize by `k-1`, where `k` is the window length. If `k=1`, the weight is `k`.

• `1` — Normalize by `k`.

Data Types: `single` | `double`

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 `movvar` operates along, that is, the direction in which the specified window slides.

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

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

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

Missing value condition, specified as one of these values:

• `"includemissing"` or `"includenan"` — Include `NaN` values in `A` when computing each variance. 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 variance 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 = movvar(A,k,"Endpoints","fill")`

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 variance 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.

Sample points for computing variances, 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 `movvar(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"`.

collapse all

### Variance

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

`$V=\frac{1}{N-1}\sum _{i=1}^{N}|{A}_{i}-\mu {|}^{2}$`

where μ is the mean of A,

`$\mu =\frac{1}{N}\sum _{i=1}^{N}{A}_{i}.$`

Some definitions of variance use a normalization factor of N instead of N-1. You can specify a factor of N by setting `w` to `1`. In either case, the mean is assumed to have the usual normalization factor N.

## Version History

Introduced in R2016a

expand all