User defined functions for standard deviation
16 次查看(过去 30 天)
显示 更早的评论
Hi. I'm trying to come up with a function that calculates the standard deviation of an array. I understand there's a built in function that does this but I'm not allowed to use it for my class.
Here's what I have so far,I seem to have hit a wall and would really appreciate any help or suggestions.
Thanks!!! :)
x = [1 2 3 4];
sum = 0;
k = length(x);
for i = 1: k
sum = sum+x(i);
mean = (sum)/k;
stdev = ((x(i)-mean).^2);
stdev = sqrt((stdev)/k);
end
1 个评论
回答(1 个)
Azzi Abdelmalek
2013-3-13
编辑:Azzi Abdelmalek
2013-3-13
sum and mean are built-in functions, use instead for example som and moy.
You should use two for loop, the first to calculate the mean of x, and the second to calculate the std of x. Note that the Matlab function std calculate
sqrt(sum((xi-mean(x))^2/(n-1))) % n is the length of x
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!