- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

MATLABData AnalysisProgramming

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

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 vectorA = [2, 4, 5, 7, 10];% Calculate the standard deviationS = std(A);% Display the input vector and the standard deviationdisp('The input vector is:');disp(A);disp('Simple Standard Deviation of A is:');disp(S);

### Output

The input vector is: 2 4 5 7 10Simple 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 vectorA = [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 deviationS = std(A, w);% Display the input vector and the weighted standard deviationdisp('The input vector is:');disp(A);disp('Weighted Standard Deviation of A is:');disp(S);

### Output

The input vector is: 2 4 5 7 10Weighted 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 arrayA = [1, 2, 3; 5, 7, 7; 10, 13, 15];% Calculating weighted standard deviation for all elements of AS = std(A, 0, 'all');% Display the input array and its standard deviationdisp('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 15Standard 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 arrayA = [1, 2, 5; 6, 9, 7; 8, 4, 3];% Create a weight vectorw = [1.5, 1, 2.5];% Calculating standard deviation along the rows S_r = std(A, w, 1);% Calculating standard deviation along the columnsS_c = std(A, w, 2);% Display the input array and the standard deviation along both dimensionsdisp('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 3Standard Deviation along rows: 3.0414 2.4576 1.5620Standard 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.

Manish Kumar Saini

**e**

Updated on: 07-Aug-2023

267 Views

- Related Articles
- How to calculate standard deviation in Excel?
- C++ Program to Calculate Standard Deviation
- Python Program to Calculate Standard Deviation
- Golang Program to Calculate Standard Deviation
- Swift Program to Calculate Standard Deviation
- Java Program to Calculate Standard Deviation
- C program to calculate the standard deviation
- Python - Calculate the standard deviation of a column in a Pandas DataFrame
- Variance and Standard Deviation
- How to Calculate Covariance in MATLAB
- How to Calculate Variance in MATLAB?
- How to create boxplot using mean and standard deviation in R?
- How to find the moving standard deviation in an R matrix?
- How to Calculate Cumulative Product in MATLAB
- How to Calculate Moving Sum in MATLAB?

##### Kickstart Your Career

Get certified by completing the course

Get Started

Advertisem*nts

';adpushup.triggerAd(ad_id); });