Documentation

std

Standard deviation

Syntax

Description

example

S = std(A) returns the standard deviation of the elements of A along the first array dimension whose size does not equal 1.

  • If A is a vector of observations, then the standard deviation 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 deviations corresponding to each column.

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

  • By default, the standard deviation is normalized by N-1, where N is the number of observations.

example

S = std(A,w) specifies a weighting scheme for any of the previous syntaxes. When w = 0 (default), S is normalized by N-1. When w = 1, S is normalized by the number of observations, N. 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.

example

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

example

S = std(___,nanflag) specifies whether to include or omit NaN values from the calculation for any of the previous syntaxes. For example, std(A,'includenan') includes all NaN values in A while std(A,'omitnan') ignores them.

Examples

collapse all

Standard Deviation of Matrix Columns

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 =

    7.0000    4.1633    3.0551

Standard Deviation of 3-D Array

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(:,:,1) =

    2.8284    2.1213


S(:,:,2) =

    9.8995    4.2426


S(:,:,3) =

    2.8284    4.9497

Specify Standard Deviation Weights

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 =

    4.4900    1.8330

Standard Deviation Along Matrix Rows

Create a matrix and calculate 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 =

   11.0303
    9.4692
    5.3229

Standard Deviation Excluding NaN

Create a vector and compute its standard deviation, excluding NaN values.

A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19];
S = std(A,'omitnan')
S =

    2.2797

Input Arguments

collapse all

A — Input arrayvector | matrix | multidimensional array

Input array, specified as a vector, matrix, or multidimensional array. 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
Complex Number Support: Yes

w — Weight0 (default) | 1 | vector

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

dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 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 a two-dimensional input array, A.

  • If dim = 1, then std(A,0,1) returns a row vector containing the standard deviation of the elements in each column.

  • If dim = 2, then std(A,0,2) returns a column vector containing the standard deviation of the elements in each row.

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

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

nanflagNaN condition'includenan' (default) | 'omitnan'

NaN condition, specified as one of these values:

  • 'includenan' — Include NaN values when computing the standard deviation, resulting in NaN.

  • 'omitnan' — Ignore NaN values appearing in either the input array or weight vector.

Data Types: char

More About

collapse all

Standard Deviation

For a random variable vector A made up of N scalar observations, the standard deviation is defined as

S=1N1i=1N|Aiμ|2,

where μ is the mean of A:

μ=1Ni=1NAi.

The standard deviation is the square root of the variance. Some definitions of standard deviation use a normalization factor of N instead of N-1, which you can specify by setting w to 1.

See Also

| | | |

Was this topic helpful?