2013-03-27 58 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