C# 将两个二维数组混合成一个二维数组
我正在尝试将int以前的数组(1和2)存储在一个新数组中的结果。3可以用每个数组2显示来自数组1的每个结果,当我分别显示每个值的结果时,它们是可以的,但是当我尝试将它们混合到数组3中时,我得到了数组1中第一个值与数组2中所有值的正确答案,然后出现了很多零 下面是数组3的代码C# 将两个二维数组混合成一个二维数组,c#,arrays,C#,Arrays,我正在尝试将int以前的数组(1和2)存储在一个新数组中的结果。3可以用每个数组2显示来自数组1的每个结果,当我分别显示每个值的结果时,它们是可以的,但是当我尝试将它们混合到数组3中时,我得到了数组1中第一个值与数组2中所有值的正确答案,然后出现了很多零 下面是数组3的代码 for (int i = 0; i < result1.GetLength(0); i++) { for (int j = 0; j < result1.GetL
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
counter++;
}
}
}
}
for(int i=0;i
这是全部代码
double[,] Cranelocations = { { -12.3256, 0.5344 }, { -12.3256, -0.4656 }, { -12.3256, -1.4656 }, { -12.3256, -2.4656 } };
double[,] Picklocation = { { -0.3256, -3.4656 }, { 0.6744, -3.4656 }, { 1.6744, -3.4656 }, { 2.6744, -3.4656 }, { 3.6744, -3.4656 }, { 4.6744, -3.4656 }, { 5.6744, -3.4656 } };
double[,] Setlocation = { { 20.62, 5.03 }, { 24.28, 5.03 }, { 28.40, 5.03 }, { 32.11, 5.03 }, { 35.99, 5.26 }, { 40.18, 5.26 } };
double[] Weights = { 11.7865, 14.7335, 15.1015, 10.7465 };
double[,] result1 = new double[Weights.Length * Cranelocations.GetLength(0), Picklocation.GetLength(0)];
double[,] result2 = new double[Weights.Length * Cranelocations.GetLength(0), Setlocation.GetLength(0)];
object[,] result3 = new object[result1.GetLength(0) * result1.GetLength(1), result2.GetLength(0) * result2.GetLength(1)];
int counter = 0;
for (int m = 0; m < Weights.Length; m++)
{
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
counter++;
}
}
}
}
}
double[,]Cranelocations={-12.3256,0.5344},{-12.3256,-0.4656},{-12.3256,-1.4656},{-12.3256,-2.4656};
双[,]选取位置={-0.3256,-3.4656},{0.6744,-3.4656},{1.6744,-3.4656},{2.6744,-3.4656},{3.6744,-3.4656},{4.6744,-3.4656},{5.6744,-3.4656};
双[,]集合位置={20.62,5.03},{24.28,5.03},{28.40,5.03},{32.11,5.03},{35.99,5.26},{40.18,5.26};
双[]权重={11.7865,14.7335,15.1015,10.7465};
double[,]result1=新的double[Weights.Length*CranelLocations.GetLength(0),Picklocation.GetLength(0)];
double[,]result2=新的double[Weights.Length*Cranelocations.GetLength(0),Setlocation.GetLength(0)];
object[,]result3=新对象[result1.GetLength(0)*result1.GetLength(1),result2.GetLength(0)*result2.GetLength(1)];
int计数器=0;
对于(int m=0;m
对于每个m
您将i
重置为0,然后再次从数组开始写入
当您增加m
时,需要继续向前移动索引
组合这两个索引时也是如此。当您组合两个迭代以获得索引时,通常将第一个迭代乘以第二个迭代的长度,然后将它们相加
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
int iOffset = i * result1.GetLength(1);
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
int kOffset = k * result2.GetLength(1);
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[iOffset + j, kOffset + m] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[iOffset + j, kOffset + m]);
counter++;
}
}
}
}
for(int m=0;m
尝试将代码放入,以便我们可以看到结果及其不起作用的原因我使用结果3的目的是将数组结果1中的每个结果与数组结果2中的每个结果放在一起,并如下所示(,)。和