Open In App

How To Calculate Standard Deviation in MATLAB?

Last Updated : 10 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Standard deviation is a statistical quantity that tells us how much-distributed data is from its mean value. Standard deviation can also be defined as the square root of the Variance of the same data.

Standard Deviation in MATLAB:

MATLAB provides a simple function to calculate the standard deviation of data, the std() function, which is very similar to the var() function which calculates the variance of data. The syntax of std() function is:

std_dev = std(<data>, <weight>, …)

Where, <data> is the data in the form of an array or vector and <weight> is an optional argument, which stores the weights of the corresponding data. 

Let us understand the std() function with examples and see how to calculate the standard deviation of different types of data.

Example 1:

Matlab




% MATLAB code for Standard Deviation
% Creating data vector
 data = 23:55;
 
% Calculating standard deviation
 std_dev = std(data);
 
 disp(std_dev)


Output:

 

Let us find the standard deviation when the same data as above have weights.

Example 2:

Matlab




% Standard deviation when the same data as weight
% Creating data vector
    data = 23:55;
% Creating weights
    w = linspace(0.5,2.1,33);
 
% Calculating standard deviation
    std_dev = std(data,w);
 
    disp(std_dev)


Output:

 

MATLAB also allows the calculation of the standard variance of a multidimensional vector along a particular dimension (less than the maximum dimension) as follows.

Example 3:

Matlab




% MATLAB code for standard variance
% of a multidimensional vector
% Creating data vector
    data = [23 55 32; 1 3 5; 9 13 8.25];
 
% Calculating standard deviation at all points
std_dev = std(data,0,"all");
disp(std_dev)
 
% Calculating standard deviation along dimension 1
std_dev = std(data,0,1);
disp(std_dev)
 
% Calculating standard deviation along dimension 2
std_dev = std(data,0,2);
disp(std_dev)


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads