Converting 3D-Array to List in C#

Krus

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

Enigmativity

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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

converting nested XML to 3d array

From Dev

Converting 2d array into 3d array in python

From Dev

Converting 2d array into 3d array in python

From Dev

Converting a list of 3d matrix into 4d matrix

From Dev

Converting a 3D int array to an Octree in Java

From Java

Converting array to list in Java

From Dev

creating a list and then converting it to array

From Dev

Converting Array and copying it into a List

From Dev

Why is extra bracket required in C++ 3d array list assignment?

From Dev

i need a help converting a 3 column list to a 3 column array

From Dev

Simple 3D Array C++

From Dev

C 3d Array of char

From Dev

Address for a 2/3d array in c

From Dev

python 3d array like in C ++

From Dev

Simple 3D Array C++

From Dev

C Dynamic 3D array memory

From Dev

Converting a 1D numpy array to a list of lists

From Dev

Converting C array ptr to native array in Swift 3

From Dev

Difference between converting List to Array

From Dev

Converting list of objects to json array

From Dev

Converting a Frege List to a Java Array

From Dev

Converting list to numpy array issues

From Dev

Unravel 1D list back to 3D array

From Dev

Converting JSON string array into c# List object returning "null" value for each field of a Model inside a List

From Java

Replace multiple values of 3d Numpy array by list of indexes

From Dev

numpy create 3D array from indexed list

From Dev

Matlab: subtract vectors in a 3D array by a list of vectors

From Dev

List of n first Neighbors from a 3d Array R

From Dev

rearrange 3d numpy array with list of indices