9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPでユークリッド距離を求める

Posted at

KMeans法を実装したくて、集合知プログラミング本を読んでいる途中のメモ。

ユークリッド距離の計算方法は以下の通り。(Wikipediaより)

ScreenClip.png

以下、コード+試験。エラー処理などは特になし。

EuclidDistance.php
<?php

function distance($data1, $data2) {
    $num = 0;
    for ($i = 0; $i < sizeof($data1); $i++) {
        $num += pow($data1[$i] - $data2[$i], 2);
    }
    return sqrt($num);
}

var_dump(distance(array(1, 1), array(4, 5)));//int(5)
9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?