2015-11-12 29 views
2

我有一個維度365x28(即365行和28列)的二維矩陣。我正在嘗試使用以下代碼進行標準化:二維數組的標準化

public static void main(String args[]) throws Exception { 
    double[][] X = new double [365][28]; 
    double[][] X_min = new double [1][28]; 
    double[][] X_max = new double [1][28]; 

    double[][] X_norm = null; 

    X_norm = normalize(X, X_min, X_max);// error in this line 

public static double[][] normalize(double[][] ip_matrix, double[][] min_bound, double[][] max_bound) 
{ 

    double[][] mat1 = ip_matrix; 

    double[][] norm = new double[mat1.length][mat1[0].length]; 

    for (int i = 0; i < mat1.length; i++) 
    { 
     for (int j = 0; j <= mat1[i].length; j++) 
     { 
       norm[i][j] = (mat1[i][j] - min_bound[i][j]/(max_bound[i][j] - min_bound[i][j]));// error in this line 
     } 
    } 

    return norm; 

}   
} 

但是,當我運行這個我得到一個索引錯誤。我知道min_bound [i] [j]和max_bound [i] [j]有問題。但我不知道如何糾正這一點。任何人都可以幫助我嗎?提前致謝。

Error: exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 
+0

如果你發佈你所得到完整的錯誤這將是有益的。 – EkcenierK

+0

yup ..我現在編輯它。 –

+0

這段代碼給了我一個'ArrayIndexOutOfBoundsException:28' – ParkerHalo

回答

4

嗯,你的問題是很明顯的,如果你考慮一下。記住數組的起始索引爲0. 因此,在第一個for循環中用var int「i」進行規範化時,它會上升到365,因爲您會檢查二維數組X的長度,其中min_bound和max_bound的大小隻有1和28想想

這工作假設min_bound和max_bound總是[1] [(一些值)]

for (int i = 0; i < mat1.length - 1; i++) { 
      for (int j = 0; j <= mat1[i].length - 1; j++) { 
       norm[i][j] = (mat1[i][j] - min_bound[0][j] 
         /(max_bound[0][j] - min_bound[0][j]));// error in this 
       // line 
      } 
     } 
1
for (int i = 0; i < mat1.length; i++) 

我去365,你叫

norm[i][j] = (mat1[i][j] - min_bound[i][j]/(max_bound[i][j] - min_bound[i][j])); 

其中min_bound是1x28

因此,你會得到indexoutofbounds例外