recipes : Statistics : Calculating the standard deviation

Problem

How do I calculate the standard deviation in MATLAB?

Solution

The easiest option is to use MATLAB's std, function, which returns the standard deviation of a distribution. You don't need the Statistics Toolbox to access this function.

data=randn(1,100); %Draw 100 random numbers from the normal distribution
std(data)
ans =

    1.0358

If you have a matrix, you can calculate the standard deviation of the rows or columns as follows:

data=randn(10,3); %Random number matrix with 10 rows and 3 columns

std(data) %SD of the 3 columns
ans =

    0.7837    0.6965    1.2933



std(data,[],2) %SD of the 10 rows

ans =

    1.6209
    0.8323
    0.6575
    0.8570
    1.4800
    0.7116
    0.5682
    0.9983
    1.3137
    0.2725
Discussion

Another useful statistic you might want to calculate is the standard error of the mean. To learn how to add the standard deviation as an error bar to bar charts see this recipe.

 

Want to continue the discussion?
Enter your comments, suggestions, or thoughts below

comments powered by Disqus