std - Standard deviation - MATLAB (original) (raw)

Syntax

Description

[S](#mw%5F9e4c3483-9813-4084-a3a9-65974f62161f) = std([A](#bune5v2-1-A)) returns the standard deviation of the elements of A along the first array dimension whose size does not equal 1. By default, the standard deviation is normalized by N-1, where N is the number of observations.

example

[S](#mw%5F9e4c3483-9813-4084-a3a9-65974f62161f) = std([A](#bune5v2-1-A),[w](#bune5v2-1-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 ofw must equal the length of the dimension over whichstd is operating.

example

[S](#mw%5F9e4c3483-9813-4084-a3a9-65974f62161f) = std([A](#bune5v2-1-A),[w](#bune5v2-1-w),`"all"`) returns the standard deviation over all elements of A whenw is either 0 or 1.

[S](#mw%5F9e4c3483-9813-4084-a3a9-65974f62161f) = std([A](#bune5v2-1-A),[w](#bune5v2-1-w),[dim](#bune5v2-1-dim)) returns the standard deviation along dimension dim. To maintain the default normalization while specifying the dimension of operation, setw = 0 in the second argument.

example

[S](#mw%5F9e4c3483-9813-4084-a3a9-65974f62161f) = std([A](#bune5v2-1-A),[w](#bune5v2-1-w),[vecdim](#mw%5F84ddaed0-ff32-4e04-ab58-c75bc8827c89)) returns the standard deviation over the dimensions specified in the vectorvecdim when w is 0 or 1. For example, ifA 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](#mw%5F9e4c3483-9813-4084-a3a9-65974f62161f) = std(___,[missingflag](#mw%5F304a430a-ab77-4b7f-adfa-090ab06c2a6d)) 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

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 = 1×3

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 = 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)

Standard Deviation Along Matrix Rows

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

Standard Deviation of Array Page

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

Standard Deviation Excluding Missing Values

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 = 1×4

     0    0.2440       NaN    2.2203

Before R2023a: Use "omitnan" or "omitnat" to ignore missing values.

Standard Deviation and Mean

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)

Input Arguments

collapse all

A — Input array

vector | matrix | multidimensional array | table | timetable

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

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

w — Weight

0 (default) | 1 | vector

Weight, specified as one of these values:

Data Types: single | double

dim — Dimension to operate along

positive integer scalar

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.

Dimension dim indicates the dimension whose length reduces to 1. The size(S,dim) is1, while the sizes of all other dimensions remain the same.

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

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

vecdim — Vector of dimensions

vector of positive integers

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. Thenstd(A,0,[1 2]) returns a 1-by-1-by-3 array whose elements are the standard deviations computed over each page ofA.

Mapping of a 2-by-3-by-3 input array to a 1-by-1-by-3 output array

missingflag — Missing value condition

"includemissing" (default) | "includenan" | "includenat" | "omitmissing" | "omitnan" | "omitnat"

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

Value Input Data Type Description
"includemissing" (since R2023a) All supported data types Include missing values inA 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" (since R2023a) All supported data types Ignore missing values inA 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

S — Standard deviation

scalar | vector | matrix | multidimensional array | table

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

M — Mean

scalar | vector | matrix | multidimensional array | table

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

If S is the weighted standard deviation, thenM is the weighted mean.

More About

collapse all

Standard Deviation

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

where μ is the mean of A:

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 of1, 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 schemew, the weighted standard deviation is defined as

where μw is the weighted mean of A.

Weighted Mean

For a random variable vector A made up of_N_ scalar observations and weighting schemew, the weighted mean is defined as

Extended Capabilities

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

This function supports tall arrays with the limitation:

For more information, see Tall Arrays for Out-of-Memory Data.

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Usage notes and limitations:

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

The std function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

R2023a: Perform calculations directly on tables and timetables

The std function can calculate on all variables within a table or timetable without indexing to access those variables. All variables must have data types that support the calculation. For more information, see Direct Calculations on Tables and Timetables.

R2023a: Specify missing value condition

Include or omit all missing values in the input arrays when computing the standard deviation by using the "includemissing" or"omitmissing" options. Previously,"includenan", "omitnan","includenat", and "omitnat" specified a missing value condition that was specific to the data type of the input arrays.

R2023a: Improved performance with small group size

The std function shows improved performance when computing over a real vector when the operating dimension is not specified. The function determines the default operating dimension more quickly in R2023a than in R2022b.

For example, this code computes the standard deviation along the default vector dimension. The code is about 1.9x faster than in the previous release.

function timingStd A = rand(10,1); for i = 1:8e5 std(A); end end

The approximate execution times are:

R2022b: 1.77 s

R2023a: 0.93 s

The code was timed on a Windows® 10, Intel® Xeon® CPU E5-1650 v4 @ 3.60 GHz test system using thetimeit function.

R2022a: Return mean or weighted mean

The std function can now return the mean of the input elements used to calculate the standard deviation by using a second output argumentM. If a weighting scheme is specified, thenstd returns the weighted mean.

R2018b: Operate on multiple dimensions

Operate on multiple dimensions of the input array at a time. Specify a vector of operating dimensions, or specify the "all" option to operate on all array dimensions.