【发布时间】:2014-02-21 18:37:57
【问题描述】:
我有两个坐标值。我正在计算每个坐标的欧几里得距离。我想在这里应用 K-最近邻域算法。
- 对于每个坐标,获取与所有其他点的欧几里得距离
-
选择最接近最大点的点(从所有点中,选择最接近最大坐标数的点)
$result2 = mysqli_query($con,"SELECT pcount, ncount from test"); $result1 = mysqli_query($con,"SELECT pcount, ncount from test"); $i = 0; $k = 0; $min = 0; while ($row1 = @mysqli_fetch_array($result1)) { $pcount = $row1['pcount']; $ncount = $row1['ncount']; echo "pcount is $pcount<br/>"; echo "ncount is $ncount<br/></br>"; $a[$i] = $pcount ; $b[$i] = $pcount ; $j = 0; $result2 = mysqli_query($con,"SELECT pcount, ncount from test"); while ($row2 = @mysqli_fetch_array($result2)) { //echo "j is $j <br/>"; $a[$j] = $row2['pcount']; $b[$j] = $row2['ncount']; $diff = ($a[$i] - $a[$j])^2 + ($b[$i] - $b[$j])^2; if( $diff != 0) $diff = sqrt( $diff ); $j= $j + 1; echo "$diff <br>"; arr[$k] = $diff; $k = $k + 1; } //echo "i is $i <br/>"; $i = $i + 1; }
这里,arr[$k] 包含所有点的距离差。
如何在这里获得最接近最大其他点的点。可能有超过 1 个这样的点。
【问题讨论】:
-
为什么要执行两个相同的数据库查询?
$result1和$result2代表相同的数据。所以你只是浪费内存。您必须执行一个查询,获取它的结果为一个数组并在循环中使用该数组。
标签: php arrays algorithm data-structures knn