JavaScript之数值计算

//两等长数组对应元素之间做减法运算【可拓展:基本运算(+/-*//)】
function array_dif(length,arrayA,arrayB){
var array = new Array();
for(var i = 0;i<length;i++){
array.push(arrayA[i]-arrayB[i]);
//console.log(arrayA[i]-arrayB[i]);
}
return array;
}
real = [181,135,61,63,53,60,51,5];
interests = [175,145,71,66,57,59,62,9];
array_dif(8,interests,real);
function array_euclideanMetric(size,pointsA,pointsB){//求欧式距离
var array = new Array();
for(var i = 0;i<size;i++){
array.push(Math.sqrt(Math.pow(pointsA[i][0] - pointsB[i][0],2)+Math.pow(pointsA[i][1] - pointsB[i][1],2)));
console.log("第"+i+":" + Math.sqrt(Math.pow(pointsA[i][0] - pointsB[i][0],2)+Math.pow(pointsA[i][1] - pointsB[i][1],2)));//
}
return array;
} real_points = [[1,181],[2,135],[3,61],[4,63],[5,53],[6,60],[7,51],[8,5]];
interests_points = [[1,175],[2,145],[3,71],[4,66],[5,57],[6,59],[7,62],[8,9]];
array_euclideanMetric(8,interests_points,real_points);

  

function simularity_byEuclideanMetric(size,rate,baseArray,euclideanMetricArray){//通过计算点之间的欧式距离,计算坐标点数组的两相似度[rate:判断为相似点的阈值;注意:x轴值统一不变]
  var count = 0; //计数器:相似的点
  var array = [];
  for(var i = 0;i<size;i++){
    if(rate <= Math.abs(baseArray[i]-euclideanMetricArray[i])/baseArray[i]){
      array.push(1);
      count++;
    }
    else
      array.push(0);
  }
  return count/size;
}

  

function array_euclideanMetric(size,pointsA,pointsB){//求欧式距离
var array = new Array();
for(var i = 0;i<size;i++){
array.push(Math.sqrt(Math.pow(pointsA[i][0] - pointsB[i][0],2)+Math.pow(pointsA[i][1] - pointsB[i][1],2)));
//console.log("第"+i+":" + Math.sqrt(Math.pow(pointsA[i][0] - pointsB[i][0],2)+Math.pow(pointsA[i][1] - pointsB[i][1],2)));//
}
return array;
} //两等长数组对应元素之间做减法运算【可拓展:基本运算(+/-*//)】
function array_dif(length,arrayA,arrayB){
var array = new Array();
for(var i = 0;i<length;i++){
array.push(arrayA[i]-arrayB[i]);
//console.log(arrayA[i]-arrayB[i]);
}
return array;
} function simularity_byEuclideanMetric(size,rate,baseArray,euclideanMetricArray){//通过计算点之间的欧式距离,计算坐标点数组的两相似度[rate:判断为相似点的阈值;注意:x轴值统一不变]
var count = 0; //计数器:相似的点
var array = [];
for(var i = 0;i<size;i++){
if(rate <= Math.abs(baseArray[i]-euclideanMetricArray[i])/baseArray[i]){
array.push(1);
count++;
}
else
array.push(0);
}
return count/size;
} real = [181,135,61,63,53,60,51,5];
interests = [175,145,71,66,57,59,62,9];
real_points = [[1,181],[2,135],[3,61],[4,63],[5,53],[6,60],[7,51],[8,5]];
interests_points = [[1,175],[2,145],[3,71],[4,66],[5,57],[6,59],[7,62],[8,9]]; euclideanMetrics = array_euclideanMetric(8,interests_points,real_points);
rate = 0.85;
simularity_byEuclideanMetric(real.length,rate,interests,euclideanMetrics);

  

上一篇:SQL Server 更新统计信息出现严重错误,应放弃任何可能产生的结果


下一篇:Python:列表操作总结