# Standard Deviation in Matlab – Syntax and Example

September 21, 2021 2021-10-04 7:21## Standard Deviation in Matlab – Syntax and Example

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.

- 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.

## 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 `A`

,`w`

)`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.

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 `A`

,`w`

,`dim`

)`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 `A`

,`w`

,`vecdim`

)`vecdim`

when `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×37.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×111.0303 9.4692 5.3229

## In Conclusion

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.

*Are you interested in working with data? Get a free trial month of LinkedIn Learning and learn Data Science and Machine Learning courses using Python, R, and SQL from top instructors. *

** Enroll in any one of the courses from the following fields today on LinkedIn Learning and start your free month of learning:**

*Disclaimer: When you subscribe to LinkedIn Learning, we may earn a small commission as an affiliate. We love the platform so much so that we have been using it ourselves at The Click Reader. *