Saleh Parsa
Saleh Parsa

Reputation: 1433

Combine two double jagged array in one jagged array in C#

I'm doing my project and I'm facing to a problem with combining two jagged arrays and create one.

Below is a example: Jagged one :

double[][] JaggedOne=
{
    new double[] { -5, -2, -1 },
    new double[] { -5, -5, -6 },

};

and below is my second one:

double[][] JaggedTwo=
{
    new double[] {1, 2, 3 },
    new double[] { 4, 5, 6 },
};

Now as a result I want this:

double[][] Result =
{
    {-5,-2,-1},
    {-5,-5,-6},
    {1,2,3},
    {4,5,6},
};

Actually the first one im loading from the XML file and the second one and it is my training set and the second one is my sample test for using in machine learning . I really appreciate for your reply and tips.

Upvotes: 0

Views: 1844

Answers (2)

Mukund
Mukund

Reputation: 1689

There is one more feature of Union available. You can use that also. Like -

class JaggedArray {

        public static void Main(string[] args)
        {
            double[][] JaggedOne = {
                                        new double[] { -5, -2, -1 },
                                        new double[] { -5, -5, -6 },
                                   };

            double[][] JaggedTwo = {
                                        new double[] {1, 2, -1 },
                                        new double[] { 4, 5, 6 },
                                   };

            double[][] result = JaggedOne.Union(JaggedTwo).ToArray();           

            for (int i = 0; i < result.Length; i++)
            {
                for (int j = 0; j < result[i].Length; j++)
                {
                    Console.Write(result[i][j]);
                }

                Console.WriteLine();
            }            

            Console.ReadKey();
        }
}    

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499860

The fact that they're jagged arrays is actually irrelevant here - you've really just got two arrays which you want to concatenate. The fact that the element type of those arrays is itself an array type is irrelevant. The simplest approach is to use LINQ:

double[][] result = jaggedOne.Concat(jaggedTwo).ToArray();

Upvotes: 8

Related Questions