1

For easily finding values in a huge Array I want to use the "find"-Command of a List. The problem is, right now it is not a List, it is a 3D-Array.

This means, i need to convert my double[,,]-array to a

List<List<List<double>>> blabla

Example of an Array:

double[,,] myArray = new double[2,3,3];

Could look then like the following:

myArray[1,:,:] =  1.11 1.12 1.13
                  1.21 1.22 1.23
                  1.31 1.32 1.33

myArray[2,:,:] =  2.11 2.12 2.13
                  2.21 2.22 2.23
                  2.31 2.32 2.33

Can this be done by a one-liner using somehow the "Cast" command?

Thanks

6
  • can you add a sample array Commented Sep 1, 2014 at 1:54
  • Multidimensional arrays have very few uses outside pure maths, what are you trying to accomplish? Commented Sep 1, 2014 at 2:13
  • It will be really hard, if possible at all, to get a one-liner without creating your own method and calling it, but I don't think that's what you meant Commented Sep 1, 2014 at 2:21
  • @Mitch Actually I use it for an optimization algorithm, which is quite math-based. But I often have to find some values or check whole rows of this array, which is much more easy using a List. But if there is no easy conversion possibility, I will probably change from multidimensional array to a List :) Thanks for the help Commented Sep 1, 2014 at 2:36
  • @MarcinJuraszek - Challenge accepted! Please see my "one-liner" answer. ;-) Commented Sep 1, 2014 at 4:01

3 Answers 3

1

Given your double[,,] myArray you can do this:

var blabla = 
    myArray
        .Cast<double>()
        .Select((v, n) => new
        {
            x = n / (myArray.GetLength(2) * myArray.GetLength(1)),
            y = (n / myArray.GetLength(2)) % myArray.GetLength(1),
            value = v,
        })
        .GroupBy(q => q.x)
        .Select(q =>
            q
                .GroupBy(
                    w => w.y,
                    w => w.value)
                .Select(e => e.ToList())
                .ToList())
        .ToList();

That gives you the List<List<List<Double>>> that you're looking for.

And all in a one-liner!

Sign up to request clarification or add additional context in comments.

Comments

0

you can do it. you have to loop the items and get the result. check the sample code

double[, ,] array3D = new double[2, 3, 3] { { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }, { { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 18 } } };


        List<List<List<double>>> topLevel = new List<List<List<double>>>();

        for (int a = 0; a < array3D.GetLength(0); a++)
        {
            List<List<double>> secondLevel = new List<List<double>>();
            for (int b = 0; b < array3D.GetLength(1); b++)
            {
                List<double> thirdLevel = new List<double>();
                for (int c = 0; c < array3D.GetLength(2); c++)
                {
                    thirdLevel.Add(array3D[a, b, c]);
                }
                secondLevel.Add(thirdLevel);
            }
            topLevel.Add(secondLevel);
        }

Output: enter image description here

Comments

0

Not quite one line, but Linq helps:

var l = new List<List<List<double>>>(
    Enumerable.Range(0, array3D.GetLength(0)).Select(i0 => new List<List<double>>(
        Enumerable.Range(0, array3D.GetLength(1)).Select(i1 => new List<double>(
            Enumerable.Range(0, array3D.GetLength(2)).Select(i2 => array3D[i0, i1, i2])
        ))
    )));

I still would emphasize that this seems to be something which would be better suited to a custom class. The names Matrix, Sequence and Set come to mind...

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.