Standard deviation (SD or σ) can be computed using the
std() function in Matlab. In this article, you will learn various ways to perform standard deviation in Matlab.
Standard Deviation in Matlab – Syntax
The syntax for computing standard deviation in Matlab is as follows:
S = std(A)
The syntax above returns the standard deviation of the elements of
A along the first array dimension whose size does not equal 1.
Ais a vector of observations, then the standard deviation is a scalar.
Ais a matrix whose columns are random variables and whose rows are observations, then
Sis a row vector containing the standard deviations corresponding to each column.
Ais 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
1while the sizes of all other dimensions remain the same.
- By default, the standard deviation is normalized by
Nis the number of observations.
Adding a weighing scheme to standard deviation in Matlab
The syntax for adding a weighing scheme when computing standard deviation in Matlab is as follows:
S = std(A,w)
S = std(A,w,'all') S = std(A,w,dim) S = std(A,w,vecdim)
S = std( specifies a weighting scheme for any of the previous syntaxes. When
w = 0 (default),
S is normalized by
w = 1,
S 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’) computes the standard deviation over all elements of A when w is either 0 or 1. This syntax is valid for MATLAB® versions R2018b and later.
S = std( 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.
S = std( computes the standard deviation over the dimensions specified in the vector
w is 0 or 1. For example, if
A is a matrix, then
std(A,0,[1 2]) computes the standard deviation over all elements in
A, since every element of a matrix is contained in the array slice defined by dimensions 1 and 2.
Standard Deviation in Matlab Examples
Here are some examples displaying how to compute standard deviation in Matlab:
- Standard Deviation of a Matrix in Matlab:
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 in Matlab:
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:
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
You now know how to perform standard deviation in Matlab. If you have any questions, please feel free to comment them down below and we will get back to you.
Do you want to learn Python, Data Science, and Machine Learning while getting certified? Here are some best selling Datacamp courses that we recommend you enroll in:
- Introduction to Python (Free Course) - 1,000,000+ students already enrolled!
- Introduction to Data Science in Python- 400,000+ students already enrolled!
- Introduction to TensorFlow for Deep Learning with Python - 90,000+ students already enrolled!
- Data Science and Machine Learning Bootcamp with R - 70,000+ students already enrolled!