2013-03-27 70 views
0

我要计算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 

能有人帮 例子我纠正这个功能?

+0

你的意思是你想从另一个减去一个矩阵? – 2013-03-27 21:10:39

+0

是的,就像上面的例子 – 2013-03-27 21:15:03

回答

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"); 
} 
} 
+0

这是我的概率,两个矩阵的行和列是不一样的 – 2013-03-27 21:19:05

+2

如果两个矩阵的行和列的数目不同,那么它们不能被减去......这是根本矩阵相减规则.. !!! – 2013-03-27 21:25:39

+0

以及我想从第二个矩阵中的所有行的第一个矩阵中减去每一行。 – 2013-03-27 21:37:00