My calculated value for standard deviation without the std command is about a tenth of a point off

4 views (last 30 days)
So I am trying to calculate standard deviation without using the 'std' command. The code is executing, but the final values for my standard deviation are about a tenth of a point off of what they should be. A
This is the essential code:
while i=1 < length(Feb)
FtotalVar = FtotalVar + ((Faverage - Feb(i))^2)/length(Feb);
i = i + 1;
end
FSDev = FtotalVar^.5;
Feb is a vector with a given input, 28 doubles long. Faverage is the average of the vector. The value that I am getting for the standard deviation is 11.634 while I checked it against the std function which gave me 11.86. Any ideas?

Answers (1)

Star Strider
Star Strider on 17 Mar 2015
You have to divide by (n-1), not n:
Example:
x = randi(100, 1, 50);
stdfcn = @(x) sqrt(sum((x-mean(x)).^2)/(length(x)-1));
compare = [std(x) stdfcn(x)];

Categories

Find more on Signal Generation and Preprocessing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!