欧氏距离计算

一、不转换成rdd

val pointsWithIndex=list.asScala.toList.zipWithIndex
    val dis = pointsWithIndex.flatMap(a => pointsWithIndex.filter(_._2 > a._2).map((a, _)))
  .map({ case (a, b) => (a._1.getTrackletID, b._1.getTrackletID, euclidean(a._1.getFeatureVector, b._1.getFeatureVector)) })

二、精确的欧氏距离

def euclidean(x: Array[Float], y: Array[Float]) :Double= {
   var distance = 0.0;
        for (i <- 0 until x.length) {
            var temp = Math.pow((x(i) - y(i)), 2);
            distance += temp;
        }
        distance = Math.sqrt(distance);
        return 1.0 / (1.0 + distance);
  }

你可能感兴趣的:(scala,算法)