与 php 的统计比较

statistical comparisons with php

我正在寻找可以指导我编写一些统计比较代码的正确方向的人。目前我查询我的数据库,并将按如下方式取回数据:

主要数据集:

3,4,7,10,5,8,1,3,7

要比较的集合可以是这样的,并且可以有多个集合。

4,5,6,9,10,2,3,4,6

现在我需要计算出这两组数据之间的差异——例如,3-4之间的差异是1。然后我需要选择最大的差异、最认同的和最低的分数。

你会如何处理这个编码?

$max_diff = 0;
for ($i = 0; $i < (min(count($array1), count($array2));$i++){
 if (($array1[$i]-$array2[$i]) > $max_diff ) $max_diff = $array1[$i]-$array2[$i];
}

echo $max_diff;

类似的东西...没有实际测试过,但就是这个想法。

我建议使用函数 array_walk(),将第一个数组作为第一个参数传递,将第二个数组作为第三个可选参数传递。它看起来像这样:

<?php

$array_1 = array(3,4,7,10,5,8,1,3,7);
$array_2 = array(4,5,6,9,10,2,3,4,6);
    // making a copy, because the callback function
    // works on the actual value
$array_1_copy = $array_1;

echo '<pre>';
array_walk($array_1_copy, 'difference', $array_2);
echo "\nThe maximum difference is: ". max($array_1_copy);
echo "\nThe minimum difference is: ". min($array_1_copy);
echo '</pre>';

    // the callback function, takes the 1st param as reference
function difference(&$value_1, $index_1, $array_2) {
    $difference = abs($value_1 - $array_2[$index_1]);
    echo "Difference between $value_1 and {$array_2[$index_1]} is $difference\n";
    $value_1 = $difference;
}

?>

这段代码的输出是:

Difference between 3 and 4 is 1
Difference between 4 and 5 is 1
Difference between 7 and 6 is 1
Difference between 10 and 9 is 1
Difference between 5 and 10 is 5
Difference between 8 and 2 is 6
Difference between 1 and 3 is 2
Difference between 3 and 4 is 1
Difference between 7 and 6 is 1

The maximum difference is: 6
The minimum difference is: 1