二维数组

时间:2013-03-27 21:09:16

标签: java

我想计算2矩阵元素之间的差异: 例如:

s1 = tuple[0][0]-vector[0][0]+tuple[0][1]-vector[0][1]
s2 = tuple[0][0]-vector[1][0]+tuple[0][1]-vector[1][1]
s3 = tuple[0][0]-vector[2][0]+tuple[0][1]-vector[2][1]

static double Distance1(double[][] tuple, double[][] vector)
{
    double sumSquaredDiffs =0.0;
    int i;
    int j;
    for(i=0; i<tuple.length;i++)
        for(j=0; j<vector.length; j++){
            sumSquaredDiffs = tuple[i][0] - vector[j][0]+ tuple[i][1] - vector[j][1];
        }
        return sumSquaredDiffs;
    }
}

我希望结果看起来像这样:

13.0
125.0
123.0
10.0
122.0
120.0

有人可以帮我纠正这个功能吗?

1 个答案:

答案 0 :(得分:0)

您可以按以下步骤操作:

//Assuming that no. of rows and columns in both matrices are same 
static double Distance1(double[][] arr1, double[][] arr2)
{
  double[][] result = new double[arr1.length][arr2[0].length];
  for (int i = 0 ; i < arr1.length ; i++)
  {
    for (int j = 0 ; j < arr1[i].length ; j++)
    {
        result[i][j] = arr1[i][j] - arr2[i][j];
    }
  }
 //Printing Result
 for (int i = 0 ; i < result.length ; i++)
 {
    for (int j = 0 ; j < result.length ; j++)
    {
        System.out.print(result[i][j]+"\t");
    }
    System.out.print("\n");
 }
}