San*_*Arv 2 java arrays normalization
我有一个尺寸为 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;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我得到一个索引错误。我知道 min_bound[i][j] 和 max_bound[i][j] 有问题。但我不知道如何纠正这一点。任何人都可以帮我解决这个问题吗?提前致谢。
Error: exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
Run Code Online (Sandbox Code Playgroud)
如果你仔细想想,你的问题就很明显了。记住数组从 0 开始索引。所以在第一个 for 循环中使用 var int "i" 的方法 normalize 中,它上升到 365,因为你检查了 2D 数组 X 的长度,其中 min_bound 和 max_bound 的大小只有 1和 28. 想想
假设 min_bound 和 max_bound 总是 [1][(some value)]
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2199 次 |
| 最近记录: |