首页 > 解决方案 > 将两个二维数组混合为 1 个二维数组

问题描述

我正在尝试将 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)];

            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++;

                        }
                    }

                }

            }

}

标签: c#arrays

解决方案


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

推荐阅读