Documentation

std

Standard deviation

Syntax

S = std(A)
S = std(A,w)
S = std(A,w,dim)
S = std(___,nanflag)

Description

example

S = std(A)returns thestandard deviationof the elements ofAalong the first array dimension whose size does not equal 1.

  • IfAis a vector of observations, then the standard deviation is a scalar.

  • IfAis a matrix whose columns are random variables and whose rows are observations, thenSis a row vector containing the standard deviations corresponding to each column.

  • IfAis a multidimensional array, thenstd(A)operates along the first array dimension whose size does not equal 1, treating the elements as vectors. The size of this dimension becomes1while the sizes of all other dimensions remain the same.

  • By default, the standard deviation is normalized byN-1, whereNis the number of observations.

example

S = std(A,w)specifies a weighting scheme for any of the previous syntaxes. Whenw = 0(default),Sis normalized byN-1. Whenw = 1,Sis normalized by the number of observations,N.walso can be a weight vector containing nonnegative elements. In this case, the length ofwmust equal the length of the dimension over whichstdis operating.

example

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

example

S = std(___,nanflag)specifies whether to include or omitNaNvalues from the calculation for any of the previous syntaxes. For example,std(A,'includenan')includes allNaNvalues inAwhilestd(A,'omitnan')ignores them.

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×37.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 vectorw.

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

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 =3×111.0303 9.4692 5.3229

Create a vector and compute its standard deviation, excludingNaNvalues.

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

Input array, specified as a vector, matrix, or multidimensional array. IfAis a scalar, thenstd(A)returns0. IfAis a0-by-0empty array, thenstd(A)returnsNaN.

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

Weight, specified as one of these values:

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

  • 1— Normalize byN.

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

Data Types:single|double

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.

Dimensiondimindicates the dimension whose length reduces to1. Thesize(S,dim)is1, while the sizes of all other dimensions remain the same.

Consider a two-dimensional input array,A.

  • Ifdim = 1, thenstd(A,0,1)returns a row vector containing the standard deviation of the elements in each column.

  • Ifdim = 2, thenstd(A,0,2)returns a column vector containing the standard deviation of the elements in each row.

Ifdimis greater thanndims(A), thenstd(A)returns an array of zeros the same size asA.

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

NaNcondition, specified as one of these values:

  • 'includenan'— IncludeNaNvalues when computing the standard deviation, resulting inNaN.

  • 'omitnan'— IgnoreNaNvalues appearing in either the input array or weight vector.

Fordatetimearrays, you can also use'omitnat'or'includenat'to omit and includeNaTvalues, respectively.

Data Types:char

More About

collapse all

Standard Deviation

For a random variable vectorAmade up ofNscalar observations, the standard deviation is defined as

S = 1 N 1 i = 1 N | A i μ | 2 ,

whereμis the mean ofA:

μ = 1 N i = 1 N A i .

The standard deviation is the square root of the variance. Some definitions of standard deviation use a normalization factor ofNinstead ofN-1, which you can specify by settingwto1.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?