How can I calculate the standard deviation of a LARGE set of values without using the' std' or 'mean' commands?

6 views (last 30 days)
I tried entering
endStandDevx = sqrt(sum((x-(AvgX))).^2/(length(x))
(since we can't use mean I found the average by finding sum over total # of vals)
However, this line gives me a far different value than
std(x)
and i'm not really sure why...
Any help would be lovely :D

Accepted Answer

James Tursa
James Tursa on 1 Nov 2016
Edited: James Tursa on 1 Nov 2016
You've got the ^2 in the wrong place in your calculation. Also, the std function uses n-1 in the denominator by default. Try this:
>> x = rand(1,10000);
>> AvgX = sum(x)/length(x);
>> endStandDevx = sqrt(sum((x-AvgX).^2)/(length(x)-1))
endStandDevx =
0.2895
>> std(x)
ans =
0.2895
  2 Comments
Paul Camerino
Paul Camerino on 1 Nov 2016
AWESOME! Thanks so much, mate. That really helped. I guess my parentheses and everything was just way out of whack, so your direction really helped and gave me the correct value. Thanks so much!
James Tursa
James Tursa on 1 Nov 2016
Sometimes it can greatly help when you put spaces within your line to make the parentheses separations more obvious.

Sign in to comment.

More Answers (1)

Adam
Adam on 1 Nov 2016
Edited: Adam on 1 Nov 2016
std divides by n-1 rather than n. You didn't post what kind of difference you are getting so I don't know if that is the only difference but it will be a difference, obviously less noticeable with a bigger sample size though. Since you say your result is far away and your sample size is large I assume this is not the only issue.
Is your average calculation correct and matching the output of mean?
The formula for std is on its help page
doc std

Categories

Find more on Parallel Computing Toolbox 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!