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

6 次查看(过去 30 天)
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

采纳的回答

James Tursa
James Tursa 2016-11-1
编辑:James Tursa 2016-11-1
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 个评论
Paul Camerino
Paul Camerino 2016-11-1
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 2016-11-1
Sometimes it can greatly help when you put spaces within your line to make the parentheses separations more obvious.

请先登录,再进行评论。

更多回答(1 个)

Adam
Adam 2016-11-1
编辑:Adam 2016-11-1
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

类别

Help CenterFile Exchange 中查找有关 Parallel Computing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by