Matrix Laboratory or MATLAB is a programming language and numeric computing environment developed byÂ MathWorks. While it is proficient with matrix operations and plotting graphs, it can be used efficiently to perform statistical operations.

Standard Deviation tells us about the deviation in the data values with regard to its mean. Mathematically, it is calculated as the square root of the variance, determined by the deviation of every point in the data set from the mean.

In this article, we will see the methods used to calculate Standard Deviation in MATLAB and how to plot it.

Also read: How to use Meshgrid in Matlab?

**nanstd**

The *nanstd* function is used to find the standard deviation after eliminating NaN or Not a Number values. Due to this elimination, it is not the most recommended method for finding the standard deviation since it can be inaccurate at times.

If the input is in the form of a vector, the function *nanstd* gives us the sample standard deviation of all the non-NaN elements of the vector. If the input is a matrix, then the output obtained by the function *nanstd* is a row vector obtained by finding the sample standard deviation of the columns after the elimination of the NaNs.

The syntax for *nanstd* is simply mentioning the function name followed by the matrix variable in parentheses.

The *magic* function creates a square matrix with random values for ease of simplification. Additionally, we have added a NaN value to the matrix as well.

Also read: How to make a table in MATLAB?

**std**

The function *std* stands for Standard Deviation. Unlike *nanstd*, this function considers the NaN values of the matrix and gives the output accordingly.

If the input is a vector, the function *std* gives us the output as a scalar. If the input is a matrix, then the output obtained is a row vector obtained by the standard deviation of the individual columns.

The syntax for *std* is simply mentioning the function name followed by the variable in parentheses.

The standard deviation of the vector x is a scalar quantity, that is, a single value. While for matrix A, the standard deviation of the first column is obtained as NaN due to the NaN value in the matrix.

However, if you want to omit the NaN values and obtain the standard deviation for a vector or matrix, use the keyword *‘omitnan’* in the function call.

The NaN values of both the vector and matrix are omitted, and the standard deviation is obtained.

Also read: How to make and transpose a Matrix in MATLAB?

**std2**

The *std2* function computes the standard deviation of matrices only. While *nanstd* and *std* give a row vector with individual column standard deviations, *std2* gives a single scalar value of the standard deviation of the entire matrix.

The syntax for *std2* is simply mentioning the function name followed by the variable in parentheses.

If there is any NaN value in the matrix, then the final output obtained is also NaN.

Also read: How to plot an equation in Matlab?

**Plotting Standard Deviation**

Plotting standard deviation is almost similar to plotting regular graphs. Based on the function being used, ensure that you use the correct parameters for the plot.

**plot**

The function *plot* creates a simple 2-dimensional line plots of Y vs X, in our case, the standard deviation vs the matrix.

If the standard deviation is in the form of a scalar value, it can be plotted against any other scalar value, but not the matrix in itself due to parameter differences. The *mean2* function calculates a singular mean value of the entire matrix. The ‘o’ represents the point plotted in the graph of mean versus standard deviation.

**bar**

The *bar* function plots a bar graph at the specified locations by x, in our case, the matrix. The plot below is the matrix vs the standard deviation.

For scalar values, the function *bar* requires two variables with the same parameter length; hence the plot is plotted between the mean and the standard deviation, not the matrix.

**errorbar**

The *errorbar* function creates a line plot but additionally draws a vertical error bar at each data point. In the graph plotted between the mean and standard deviation below, there are 4 points plotted, and on each point, a blue vertical line can be observed.

For the graph plotted between the scalar values of mean and standard deviation, there is only 1 point on the graph, and hence there is only one vertical error bar seen.

The length of the errorbar can be changed based on our preferences.

Initialise an additional variable *err* and declare it with any value. While calling the *errorbar* function, add the variable *err* to the end.

The length of the error bar is 10 in the above graph, that is, five below the point and five above.

Also read: How to plot multiple lines in Matlab?

A creative nerd, TT player, an avid reader, and an engineering student.