Alaa Magdy
Alaa Magdy

Reputation: 35

mix two two-dimensional arrays into 1 two-dimensional array

i am trying the results which stored int previous arrays (1 and 2) in a new array 3 can show each result from array 1 with with each array 2, when i show the results from each one separately they are okay but when i try to mix them in array 3 i get the correct answers for the first value in array 1 with all values in array 2 then a lot of zeros appears.

here is the code for the array 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++;
                        }
                    }

                }

            }

and here is the whole code

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

                        }
                    }

                }

            }

}

Upvotes: 2

Views: 71

Answers (1)

Jerry
Jerry

Reputation: 1527

For each m you reset i to 0 and start writing at the beginning of your array again.

You need to keep moving the index forward when you increment m.

The same is when you are combining the two indexes. When you are combining two iterations to get an index you generally multiply the first by the length of the second, then add them together.

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

Upvotes: 1

Related Questions