# std

Standard deviation

## Description

example

S = std(A) returns the standard deviation of the elements of A along the first array dimension whose size is greater than 1. By default, the standard deviation is normalized by N-1, where N is the number of observations.

• If A is a vector of observations, then S is a scalar.

• If A is a matrix whose columns are random variables and whose rows are observations, then S is a row vector containing the standard deviation corresponding to each column.

• If A is a multidimensional array, then std(A) operates along the first array dimension whose size is greater than 1, treating the elements as vectors. The size of S in this dimension becomes 1, while the sizes of all other dimensions are the same as in A.

• If A is a scalar, then S is 0.

• If A is a 0-by-0 empty array, then S is NaN.

• If A is a table or timetable, then std(A) returns a one-row table containing the standard deviation of each variable. (since R2023a)

example

S = std(A,w) specifies a weighting scheme. When w = 0 (default), the standard deviation is normalized by N-1, where N is the number of observations. When w = 1, the standard deviation is normalized by the number of observations. w also can be a weight vector containing nonnegative elements. In this case, the length of w must equal the length of the dimension over which std is operating.

S = std(A,w,"all") returns the standard deviation over all elements of A when w is either 0 or 1.

example

S = std(A,w,dim) returns the standard deviation along dimension dim. To maintain the default normalization while specifying the dimension of operation, set w = 0 in the second argument.

example

S = std(A,w,vecdim) returns the standard deviation over the dimensions specified in the vector vecdim when w is 0 or 1. For example, if A is a matrix, then std(A,0,[1 2]) returns the standard deviation over all elements in A because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

S = std(___,missingflag) specifies whether to include or omit missing values in A for any of the previous syntaxes. For example, std(A,"omitmissing") ignores all missing values when computing the standard deviation. By default, std includes missing values.

example

[S,M] = std(___) also returns the mean of the elements of A used to calculate the standard deviation. If S is the weighted standard deviation, then M is the weighted mean.

## Examples

collapse all

Create a matrix and compute the standard deviation of each column.

A = [4 -5 1; 2 3 5; -9 1 7];
S = std(A)
S = 1×3

7.0000    4.1633    3.0551

Create a 3-D array and compute the standard deviation along the first dimension.

A(:,:,1) = [2 4; -2 1];
A(:,:,2) = [9 13; -5 7];
A(:,:,3) = [4 4; 8 -3];
S = std(A)
S =
S(:,:,1) =

2.8284    2.1213

S(:,:,2) =

9.8995    4.2426

S(:,:,3) =

2.8284    4.9497

Create a matrix and compute the standard deviation of each column according to a weight vector w.

A = [1 5; 3 7; -9 2];
w = [1 1 0.5];
S = std(A,w)
S = 1×2

4.4900    1.8330

Create a matrix and compute the standard deviation along each row.

A = [6 4 23 -3; 9 -10 4 11; 2 8 -5 1];
S = std(A,0,2)
S = 3×1

11.0303
9.4692
5.3229

Create a 3-D array and compute the standard deviation over each page of data (rows and columns).

A(:,:,1) = [2 4; -2 1];
A(:,:,2) = [9 13; -5 7];
A(:,:,3) = [4 4; 8 -3];
S = std(A,0,[1 2])
S =
S(:,:,1) =

2.5000

S(:,:,2) =

7.7460

S(:,:,3) =

4.5735

Create a matrix containing NaN values.

A = [1.77 -0.005 NaN -2.95; NaN 0.34 NaN 0.19]
A = 2×4

1.7700   -0.0050       NaN   -2.9500
NaN    0.3400       NaN    0.1900

Compute the standard deviation of the matrix, excluding missing values. For matrix columns that contain any NaN value, std computes with the non-NaN elements. For columns in A that contain all NaN values, the standard deviation is NaN.

S = std(A,"omitmissing")
S = 1×4

0    0.2440       NaN    2.2203

Create a matrix and compute the standard deviation and mean of each column.

A = [4 -5 1; 2 3 5; -9 1 7];
[S,M] = std(A)
S = 1×3

7.0000    4.1633    3.0551

M = 1×3

-1.0000   -0.3333    4.3333

Create a matrix and compute the weighted standard deviation and weighted mean of each column according to a weight vector w.

A = [1 5; 3 7; -9 2];
w = [1 1 0.5];
[S,M] = std(A,w)
S = 1×2

4.4900    1.8330

M = 1×2

-0.2000    5.2000

## Input Arguments

collapse all

Input array, specified as a vector, matrix, multidimensional array, table, or timetable. If A is a scalar, then std(A) returns 0. If A is a 0-by-0 empty array, then std(A) returns NaN.

Data Types: single | double | datetime | duration | table | timetable
Complex Number Support: Yes

Weight, specified as one of these values:

• 0 — Normalize by N-1, where N is the number of observations. If there is only one observation, then the weight is 1.

• 1 — Normalize by N.

• Vector made up of nonnegative scalar weights corresponding to the dimension of A along which the standard deviation is calculated.

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 whose length reduces to 1. The size(S,dim) is 1, while the sizes of all other dimensions remain the same.

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

• std(A,0,1) computes the standard deviation of the elements in each column of A and returns a 1-by-n row vector.

• std(A,0,2) computes the standard deviation of the elements in each row of A and returns an m-by-1 column vector.

If dim is greater than ndims(A), then std(A) returns an array of zeros the same size as A.

Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.

Consider a 2-by-3-by-3 input array, A. Then std(A,0,[1 2]) returns a 1-by-1-by-3 array whose elements are the standard deviations computed over each page of A.

Missing value condition, specified as one of the values in this table.

ValueInput Data TypeDescription
"includemissing"All supported data types

Include missing values in A and w when computing the standard deviation. If any element in the operating dimension is missing, then the corresponding element in S is missing.

"includenan"double, single, duration
"includenat"datetime
"omitmissing"All supported data typesIgnore missing values in A and w, and compute the standard deviation over fewer points. If all elements in the operating dimension are missing, then the corresponding element in S is missing.
"omitnan"double, single, duration
"omitnat"datetime

## Output Arguments

collapse all

Standard deviation, returned as a scalar, vector, matrix, multidimensional array, or table

• If A is a vector of observations, then S is a scalar.

• If A is a matrix whose columns are random variables and whose rows are observations, then S is a row vector containing the standard deviation corresponding to each column.

• If A is a multidimensional array, then std(A) operates along the first array dimension whose size is not greater than 1, treating the elements as vectors. The size of S in this dimension becomes 1, while the sizes of all other dimensions are the same as in A.

• If A is a scalar, then S is 0.

• If A is a 0-by-0 empty array, then S is NaN.

• If A is a table or timetable, then S is a one-row table. (since R2023a)

Mean, returned as a scalar, vector, matrix, multidimensional array, or table.

• If A is a vector of observations, then M is a scalar.

• If A is a matrix whose columns are random variables and whose rows are observations, then M is a row vector containing the mean corresponding to each column.

• If A is a multidimensional array, then std(A) operates along the first array dimension whose size is greater than 1, treating the elements as vectors. The size of M in this dimension becomes 1, while the sizes of all other dimensions are the same as in A.

• If A is a scalar, then M is equal to A.

• If A is a 0-by-0 empty array, then M is NaN.

• If A is a table or timetable, then M is a one-row table. (since R2023a)

If S is the weighted standard deviation, then M is the weighted mean.

collapse all

### Standard Deviation

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

$\begin{array}{l}S={\sqrt{\frac{1}{N-1}\sum _{i=1}^{N}|{A}_{i}-\mu {|}^{2}}}^{},\hfill \\ \hfill \end{array}$

where μ is the mean of A:

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

The standard deviation is the square root of the variance.

Some definitions of standard deviation use a normalization factor N instead of N – 1. You can use a normalization factor of N by specifying a weight of 1, producing the square root of the second moment of the sample about its mean.

Regardless of the normalization factor for the standard deviation, the mean is assumed to have the normalization factor N.

### Weighted Standard Deviation

For a finite-length vector A made up of N scalar observations and weighting scheme w, the weighted standard deviation is defined as

${S}_{w}=\sqrt{\frac{\sum _{i=1}^{N}{w}_{i}|{A}_{i}-{\mu }_{w}{|}^{2}}{\sum _{i=1}^{N}{w}_{i}}}$

where μw is the weighted mean of A.

### Weighted Mean

For a random variable vector A made up of N scalar observations and weighting scheme w, the weighted mean is defined as

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

## Version History

Introduced before R2006a

expand all