0

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.

1
  • did you make an attempt? Commented Jan 4, 2014 at 10:17

2 Answers 2

8

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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();
        }
}    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.