为了账号安全,请及时绑定邮箱和手机立即绑定

将两个二维数组混合为 1 个二维数组

将两个二维数组混合为 1 个二维数组

C#
明月笑刀无情 2022-10-23 14:05:02
我正在尝试将 int 先前数组(1 和 2)存储在新数组中的结果 3 可以显示数组 1 中的每个结果以及每个数组 2,当我分别显示每个数组的结果时,它们还可以,但是当我尝试将它们混合在数组 3 中,我得到数组 1 中第一个值与数组 2 中所有值的正确答案,然后出现很多零。这是数组 3 的代码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 } };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)];
查看完整描述

1 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

对于每个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++;

            }

        }

    }

}

查看完整回答
反对 回复 2022-10-23
  • 1 回答
  • 0 关注
  • 119 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信