这是我正在使用的代码:
<?php
function Median($dataPoints) {
return Quartile_50($dataPoints);
}
function Quartile_25($dataPoints) {
return Quartile($dataPoints, 0.25);
}
function Quartile_50($dataPoints) {
return Quartile($dataPoints, 0.5);
}
function Quartile_75($dataPoints) {
return Quartile($dataPoints, 0.75);
}
function Quartile($dataPoints, $Quartile) {
sort($dataPoints);
$pos = (count($dataPoints)-1) * $Quartile;
$base = floor($pos);
$rest = $pos - $base;
if( isset($dataPoints[$base+1]) ) {
return $dataPoints[$base] + $rest * ($dataPoints[$base+1] - $dataPoints[$base]);
} else {
return $dataPoints[$base];
}
}
function Average($dataPoints) {
return array_sum($dataPoints)/ count($dataPoints);
}
function StdDev($dataPoints) {
if(count($dataPoints) < 2) {
return;
}
$avg = Average($dataPoints);
$sum = 0;
foreach($dataPoints as $value) {
$sum += pow($value - $avg, 2);
}
return sqrt((1/(count($dataPoints)-1))*$sum);
}
这是我收到的错误:
致命错误:未捕获错误:C:\xampp\htdocs\arraytest.php:62 中不支持的操作数类型 堆栈跟踪:#0 C:\xampp\htdocs\arraytest.php(45): Quartile(Array, 0.25) #1 C :\xampp\htdocs\arraytest.php(84): Quartile_25(Array) #2 {main} 在第 62 行的 C:\xampp\htdocs\arraytest.php 中抛出
这是第 62 行:
return $dataPoints[$base] + $rest * ($dataPoints[$base+1] - $dataPoints[$base]);
$dataPoints
是我的数组,所以我认为这是因为我无法减去两个数组。当我将减法函数替换为array_diff()
. 我是否正确输入了 Quartile() 函数?
谢谢您的帮助