How To Calculate Standard Deviation in MATLAB?


In this article, we will learn to calculate standard deviation in MATLAB. Standard deviation is a mathematical operation that gives the measure of variation in a set of data points. The standard deviation is used to study the dispersion of data around their average value.

If a set of data points has a low standard deviation, it indicates that the data points tend to nearer to the average value. Whereas, the high standard deviation specifies a large dispersion of data points out from their average value.

In MATLAB, we can calculate the standard deviation by using a built−in function named, ‘std()’. Based on different use cases, the ‘std’ function can have the following syntaxes:

Simple Standard Deviation of an Array or Vector

The ‘std’ function has the following syntax to calculate the standard deviation of a vector or an array.

S = std(A);

The following MATLAB program illustrates the use of this syntax for computing the standard deviation.

Example

% MATLAB code for calculating simple standard deviation of a vector
% Create an input vector
A = [2, 4, 5, 7, 10];
% Calculate the standard deviation
S = std(A);
% Display the input vector and the standard deviation
disp('The input vector is:');
disp(A);
disp('Simple Standard Deviation of A is:');
disp(S);

Output

The input vector is:
    2    4    5    7   10
Simple Standard Deviation of A is:
3.0496

Code Explanation

In this MATLAB code, we start by creating a vector ‘A’. Then, we calculate the standard deviation by using the default syntax of the ‘std’ function. Finally, we display the input vector and the result using the ‘disp’ function.

Standard Deviation with Weights

The following syntax of the ‘std’ function is used to calculate the standard deviation with weights of a vector:

S = std(A, w);

Here, A is the input vector, and w is the weight vector.

The implementation of this syntax of the ‘std’ function is illustrated in the following MATLAB program.

Example

% Create an input vector and weight vector
A = [2, 4, 5, 7, 10];
w = [1, 2, 1, 0.5, 2];	% weight vector must have same number of elements as the vector A
% Calculate the weighted standard deviation
S = std(A, w);
% Display the input vector and the weighted standard deviation
disp('The input vector is:');
disp(A);
disp('Weighted Standard Deviation of A is:');
disp(S);

Output

The input vector is:
     2     4     5     7    10

Weighted Standard Deviation of A is:
    2.9733

Code Explanation

In this MATLAB code, we start by creating a vector ‘A’. Next, we create a weight vector ‘w’. After that we calculate the weighted standard deviation of the vector A by using the ‘std’ function. Finally, we display the input vector and the result using the ‘disp’ function.

Standard Deviation of a Multidimensional Array for All Elements

The following syntax of the ‘std’ function is used to calculate the weighted standard deviation of a multidimensional array for all elements of the array:

S = std(A, 0, 'all');

The following MATLAB program shows the implementation of this syntax of the ‘std’ function.

Example

% MATLAB program to calculate standard deviation of a multidimensional array
% Create a multidimensional array
A = [1, 2, 3; 5, 7, 7; 10, 13, 15];
% Calculating weighted standard deviation for all elements of A
S = std(A, 0, 'all');
% Display the input array and its standard deviation
disp('The input array is:');
disp(A);
disp('Standard Deviation for all elements is:');
disp(S);

Output

The input array is:
     1     2     3
     5     7     7
    10    13    15

Standard Deviation for all elements is:
    4.8734

Code Explanation

In this MATLAB code, we start by creating a multidimensional array ‘A’. After that we calculate the standard deviation of the array A for its all elements by using the ‘std’ function with ‘all’ option. Here, w = 0 for mixed calculation is used in the ‘std’ function. Finally, we display the input array and the result using the ‘disp’ function.

Standard Deviation of an Array along a Specified Dimension

The ‘std’ function uses the following syntax to calculate the standard deviation of an array along a specified dimension, i.e. along column or row.

S = std(A, w, dim);

If, dim = 1, the standard deviation will be calculated along the rows of the array. If dim = 2, the standard deviation will be calculated along the columns of the vector.

The following MATLAB program demonstrates the implementation of this syntax of the ‘std’ function.

Example

% MATLAB program to calculate standard deviation of an array along a specific dimension
% Create an input array
A = [1, 2, 5; 6, 9, 7; 8, 4, 3];
% Create a weight vector
w = [1.5, 1, 2.5];
% Calculating standard deviation along the rows 
S_r = std(A, w, 1);
% Calculating standard deviation along the columns
S_c = std(A, w, 2);
% Display the input array and the standard deviation along both dimensions
disp('The input array is:');
disp(A);
disp('Standard Deviation along rows:');
disp(S_r);
disp('Standard Deviation along columns:');
disp(S_c);

Output

The input array is:
     1     2     5
     6     9     7
     8     4     3

Standard Deviation along rows:
    3.0414    2.4576    1.5620

Standard Deviation along columns:
    1.8330
    1.0440
    2.1932

Code Explanation

In this MATLAB code, we start by creating an input array ‘A’. Then, we create a weight vector ‘w’. Next, we calculate the standard deviation of the array A along the rows (dim = 1) and along the columns (dim = 2) by using the ‘std’ function. The results are stored in the ‘S_r’ and ‘S_c’ variables. Finally, we display the input array and the result using the ‘disp’ function.

Conclusion

Hence, this is all about calculating the standard deviation using MATLAB. MATLAB provides a built−in function ‘std()’ to calculate standard deviation of an array or vector. It has different syntaxes for different use cases. We have explained all these syntaxes of the ‘std’ function with the help of MATLAB programs in the above sections of this article.

Updated on: 07-Aug-2023

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements