C# 3.0 如何在C#3.0中创建双[,]x#u列表?

C# 3.0 如何在C#3.0中创建双[,]x#u列表?,c#-3.0,C# 3.0,我想用Excel的LinESt函数在C#(3.0)中实现多元线性回归。 基本上,我正在努力实现 =LINEST(ACL_returns!I2:I10,ACL_returns!J2:K10,FALSE,TRUE) 因此,我有如下数据 double[] x1 = new double[] { 0.0330, -0.6463, 0.1226, -0.3304, 0.4764, -0.4159, 0.4209, -0.4070, -0.2090 }; double[] x2 = new double[

我想用Excel的LinESt函数在C#(3.0)中实现多元线性回归。 基本上,我正在努力实现

=LINEST(ACL_returns!I2:I10,ACL_returns!J2:K10,FALSE,TRUE)
因此,我有如下数据

double[] x1 = new double[] { 0.0330, -0.6463, 0.1226, -0.3304, 0.4764, -0.4159, 0.4209, -0.4070, -0.2090 };
double[] x2 = new double[] { -0.2718, -0.2240, -0.1275, -0.0810, 0.0349, -0.5067, 0.0094, -0.4404, -0.1212 };
double[] y = new double[] { 0.4807, -3.7070, -4.5582, -11.2126, -0.7733, 3.7269, 2.7672, 8.3333, 4.7023 };
我必须写一个函数,它的签名是

Compute(double[,] x_List, double[] y_List)
{
   LinEst(x_List,y_List, true, true); < - This is the excel function that I will call.
}
Compute(双[,]x\u列表,双[]y\u列表)
{
LinEst(x_List,y_List,true,true)<-这是我将调用的excel函数。
}
我的问题是如何使用double[]x1和double[]x2生成double[,]x_列表

我使用的是C#3.0和framework 3.5

提前感谢

double[,]xValues=新的double[x1.Length,x2.Length];
double[,] xValues = new double[x1.Length, x2.Length];

for (int i = 0; i < x1.Length; i++)
{
    xValues[i, 0] = x1[i];
    xValues[i, 1] = x2[i];
}
对于(int i=0;i
double[,] xValues = new double[x1.Length, x2.Length];
int max = (new int[]{ x1.Length,x2.Length}).Max();
for (int i = 0; i < max; i++)
{
xValues[0, i] = x1.Length > i ? x1[i] : 0;
xValues[1, i] = x2.Length > i ? x2[i] : 0;
}
double[,]xValues=新的双精度[x1.长度,x2.长度];
int max=(新的int[]{x1.Length,x2.Length}).max();
对于(int i=0;ii?x1[i]:0;
xValues[1,i]=x2.长度>i?x2[i]:0;
}

Samir是对的,我应该在迭代器之外调用
Max()
,而不是每次迭代,我已经修改了这一点。

多维数组的一侧在您的答案和bablo的答案中都是不正确的。此外,在bablo的回答中,每次迭代调用
Max
似乎都非常慢,尤其是在元素数量很大的情况下

int max = (new int[] { x1.Length, x2.Length }).Max();
double[,] xValues = new double[2, max];

for (int i = 0; i < max; i++)
{
    xValues[0, i] = x1.Length > i ? x1[i] : 0;
    xValues[1, i] = x2.Length > i ? x2[i] : 0;
}
intmax=(新的int[]{x1.Length,x2.Length}).max();
double[,]xValues=新的double[2,最大值];
对于(int i=0;ii?x1[i]:0;
xValues[1,i]=x2.长度>i?x2[i]:0;
}