[英]How 2d array is assign to 1D array?
I am using apache commons-math library to calculate regression parameter. 我正在使用apache commons-math库来计算回归参数。 As shown in figure 2nd and 3rd line, a 2d array is assigned to single array.
如图第二和第三行所示,将2d数组分配给单个数组。 When i use same code,
当我使用相同的代码时
I am getting error: 我收到错误消息:
"Type mismatch error:cannot convert from double [][] to double []" in 3rd line but not for 2nd line.
第三行中的“类型不匹配错误:无法从double [] []转换为double []”,但第二行则无法。
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
double[] y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
double[] x = new double[6][];
x[0] = new double[]{0, 0, 0, 0, 0};
x[1] = new double[]{2.0, 0, 0, 0, 0};
x[2] = new double[]{0, 3.0, 0, 0, 0};
x[3] = new double[]{0, 0, 4.0, 0, 0};
x[4] = new double[]{0, 0, 0, 5.0, 0};
x[5] = new double[]{0, 0, 0, 0, 6.0};
regression.newSample(y, x);
why this is happening? 为什么会这样呢? and how this works?
以及它如何工作?
More details on OLS regression are given here 有关OLS回归的更多详细信息,请参见此处
Case 1 : 情况1 :
double[] y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
You are creating a 1D array with values inserting it right away. 您正在创建一维数组,其值会立即插入。 Works fine
工作正常
Case 2 : 情况2:
double[] x = new double[6][];
You are declaring a 1D array and trying to assign a 2D array. 您正在声明一个1D数组并试图分配一个2D数组。 Which is illegal.
这是非法的。
Either change your declaration to 2D or initialize with 1D 将声明更改为2D或使用1D初始化
valid declarations will be 有效的声明将是
double[] x = new double[6]; // 1D
or 要么
double[][] x = new double[6][lenghtYouWant]; //2D
After seeing the insertions later on you are wanting a 2D array. 稍后看到插入物后,您需要一个2D阵列。 To compile the below code successfully you need to change the delcaration to
要成功编译以下代码,您需要将代号更改为
double[][] x = new double[6][];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.