1

From a function I am receiving a 2 dimensional array, I want to use this array as input data to my neural network. For this to be working it has to be an element of a 3-dimensional array. When I try to do this I get an error. I'm probably doing something wrong syntax-wise but can't figure out what the solution is. Can anyone help me?

int[,] d = image.GetData();
int[,,] neuralInput = { { d } };

also tried:

int[,,] neuralInput = { d };
bool[] answers = new bool[1] { true };

which both result in this error:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0846  A nested array initializer is expected  Sandbox C:\Users\Jordy\Documents\KBS-SE3_VR- 
Rehabilitation-Data\Sandbox\Program.cs  47  Active

and:

int[,] d = image.GetData();
int[,,] neuralInput;
neuralInput[0] = d;

which results in:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0022  Wrong number of indices inside []; expected 3   Sandbox C:\Users\Jordy\Documents\KBS- 
SE3_VR-Rehabilitation-Data\Sandbox\Program.cs   47  Active

3 Answers 3

2

Every element of an int[,,] is an int - they're independent elements. It looks like what you're trying to build is an int[][,] - an array where each element is an int[,]. If you genuinely need an int[,,], you'll need to create one of the right size, and copy elements from your int[,] into it. It's possible that Buffer.BlockCopy or similar would do that, but I'd probably just write a loop... something like this:

public int[,,] Project2DInto3D(int[,] source)
{
    int cols = source.GetLength(0);
    int rows = source.GetLength(1);
    int[,,] ret = new int[1, cols, rows];
    for (int x = 0; x < cols; x++)
    {
        for (int y = 0; y < rows; y++)
        {
            ret[0, x, y] = source[x, y];
        }
    }
    return ret;
}

Full example:

using System;

public class Test
{
    public static void Main()
    {
        int[,] original = { { 5, 3 }, { 2, 1 }, { 8, 3 } };
        Console.WriteLine("Original:");
        for (int j = 0; j < 3; j++)
        {
            for (int k = 0; k < 2; k++)
            {
                Console.WriteLine($"[{j}, {k}] = {original[j, k]}");
            }
        }
        Console.WriteLine();

        var projected = Project2DInto3D(original);
        Console.WriteLine("Projected:");
        for (int i = 0; i < 1; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 2; k++)
                {
                    Console.WriteLine($"[{i}, {j}, {k}] = {projected[i, j, k]}");
                }
            }
        }
    }

    public static int[,,] Project2DInto3D(int[,] source)
    {
        int cols = source.GetLength(0);
        int rows = source.GetLength(1);
        int[,,] ret = new int[1, cols, rows];
        for (int x = 0; x < cols; x++)
        {
            for (int y = 0; y < rows; y++)
            {
                ret[0, x, y] = source[x, y];
            }
        }
        return ret;
    }
}

Output:

Original:
[0, 0] = 5
[0, 1] = 3
[1, 0] = 2
[1, 1] = 1
[2, 0] = 8
[2, 1] = 3

Projected:
[0, 0, 0] = 5
[0, 0, 1] = 3
[0, 1, 0] = 2
[0, 1, 1] = 1
[0, 2, 0] = 8
[0, 2, 1] = 3
Sign up to request clarification or add additional context in comments.

Comments

0

You've attempted to provide array initializers for both a single dimensional:

int[,,] neuralInput = { d }; // states 3 dimensions, initialized as one, invalid.

and two dimensional:

int[,,] neuralInput = { { d } };  // states 3 dimensions, (almost) initialized as two, invalid.

but no three dimensional:

int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } };

more:

Multidimensional Arrays (C# Programming Guide)

Comments

0

There are differences in C# between jagged and multi-dimensional arrays. The jagged arrays are arrays in arrays, and can be declared like that:

int[][] jagged2d = new int[][];

Jagged arrays are arrays in arrays. If you used a jagged array, it would work. You can set one of its inner arrays.

However, "simple" multi-dimensional arrays aren't meant to modify a full array inside them; you can only modify or get exactly one value at a time.

For more information, refer to jagged arrays and multidimensional arrays.

Comments

Your Answer

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