How to access the data in a variable of type double[ , ] in C#

Asked

Viewed 47 times

2

I’m studying C# and in a certain code I came across the following situation:

double[,,] R = new double[_StatesMaxCount, 3, 2];

In all the time I’ve been studying, I’ve never seen this kind of statement and I don’t know how to use it. Could someone explain to me what this kind of statement is for and what the purpose of this code is?

2 answers

3


Hello! As per language documentation this line of code declares a multidimensional array. An example:

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

I have declared a 3x2 multidimensional R array. Note that the use of the window indicates that the array will be two-dimensional.

In the case of your code an array R is declared, using 2 commas, that is, a three-dimensional array.

Access to elements of multidimensional arrays can be done, as in the example:

Console.WriteLine(R[1, 0]); //Acessando um array bidimensional na posição 1,0 e mostrando seu valor em tela

2

Complementing what Ladynoob explained, you can iterate the dimensions of a vector matrix and briefly its elements. On the basis of language, you end up having a matrix interpolated positions.

    int[,] v = new int[,] {
        {125, 634, 84, 12},
        {63, 78, 23, 3}
    };

    for(int x = 0; x < v.GetLength(0); x++) {
        for(int y = 0; y < v.GetLength(1); y++) {
            Console.WriteLine($"V => X = {x} Y = {y} V = {v[x, y]}");
        }
    }

See working on .NET Fiddle.

In the example above, I’ve iterated a two-dimensional matrix. In this example you iterate a three-dimensional matrix:

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

    for(int x = 0; x < v.GetLength(0); x++) {
        for(int y = 0; y < v.GetLength(1); y++) {
            for(int z = 0; z < v.GetLength(2); z++) {
                 Console.WriteLine($"V => X = {x} Y = {y} Z = {z} V = {v[x, y, z]}");
        }
    }

See working on .NET Fiddle.

With the function v.GetLength(x), you receive the total size of elements in one dimension x of the matrix v. For example, the property List.Count() is a mirror to the List.ToArray().GetLength(0).

Read more about the method GetLength.

There’s also a difference between vector/multidimensional matrix and vector, which would be the case:

// vetor bidimensional
int[,] a = new int[,] { {10, 50}, {100, 150}, {200, 250} };

// vetor de vetores
int[][] b = new int[][];
b[0] = new int[] {10, 50};
b[1] = new int[] {100, 150};
b[2] = new int[] {200, 250};

But the way you iterate also changes and you travel through one vector inside the other, without having to deal with dimensions in it.

In the code where your example is, we have the following method:

    private void InitRMatrix(int _StatesMaxCount)
    {
        R = new double[_StatesMaxCount, 3, 2];

        for (int i = 0; i < _StatesMaxCount; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                R[i, j, 0] = -1;
                R[i, j, 1] = -1;
            }
        }

        _Generator.StateActionMapping(R, _States, _StatesMaxCount, FinalStateIndex);
    }

_StatesMaxCount is directly the R.GetLength(0), and the maxima of the other dimensions are literal 1 and 2, so to have:

for (int j = 0; j < 3; j++)
{
    R[i, j, 0] = -1;
    R[i, j, 1] = -1;
}

j is the position in the second dimension of the vector, which only goes from zero to two (j < 3), and the 0 and 1 in the third dimension is because its size is 2.

This method will cause R be of value -1 on all elements in your matrix, in a fixed size. It is a matrix initialization method. You cannot access R without having initiated the same, as was declared:

private double[, ,] R;

Would cause a NullReferenceException. So this method exists, to start the matrix and have values in it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.