我有一组数字,如下所示。然而,我只能计算如下所示仅输出1个结果整个阵列的标准偏差:计算PHP中每5个数字的标准差
代码:
function standard_deviation_sample ($a)
{
//variable and initializations
$the_standard_deviation = 0.0;
$the_variance = 0.0;
$the_mean = 0.0;
$the_array_sum = array_sum($a); //sum the elements
$number_elements = count($a); //count the number of elements
//calculate the mean
$the_mean = $the_array_sum/$number_elements;
//calculate the variance
for ($i = 0; $i < $number_elements; $i++)
{
//sum the array
$the_variance = $the_variance + ($a[$i] - $the_mean) * ($a[$i] - $the_mean);
}
$the_variance = $the_variance/($number_elements - 1.0);
//calculate the standard deviation
$the_standard_deviation = pow($the_variance, 0.5);
//return the variance
return $the_standard_deviation;
}
$a = array(1,2,3,4,9,6,7,8,9,20,11,12,13,14,2,16,17,18,19,27);
$standard_deviation = standard_deviation_sample ($a);
echo "standard_deviation = $standard_deviation<br>";
结果:
standard_deviation = 7.10004
没有人知道如何计算标准差为每5个数字代替?这样的结果应该是:
standard_deviation_1 = 3.11448
standard_deviation_2 = 5.70088
standard_deviation_3 = 4.82701
standard_deviation_4 = 4.39318
这个答案比我的好,我会删除我的答案,并upvote它 –