我有一个尺寸为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 :(得分:4)
如果你考虑一下,你的问题就很明显了。记住数组开始索引为0。 所以在方法中使用var int“i”在第一个for循环中进行规范化,它会上升到365,因为你检查了2D数组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 :(得分: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异常