0

I am trying to initialize the values of the elements of a jagged array to 0 in the constructor of a class. While I am able to specify the values of other variables, doing so for this jagged array eludes me.

The problem is that the dimensions of the jagged array is dependant of the values and properties of another array. This is what I did, and after is the result I am trying to obtain.

public class PattRec
{
    public int[] Userspecified;
    private double[][][] W = new double[][][] {};

    public PattRec()
    {
        // Here are the specs about the jagged array
        Userspecified= new int[] { 3, 5, 1 }; 

        // Here I try to add "Userspecified.Lenght" elements to
        // the first dimension of the jagged array
        for (int i = 0; i < Userspecified.Length; i++)
        {
            W[i] = new double[][]
            {
                new double[]{},
                new double[]{},
            };
        }

        // Here I try to set the values of the elements of "W" to 0
        // The second and third dimension of the jagged arrays are
        // linked to the values in "Userspecified"
        for (int i = 1; i < Userspecified.Length; i++)
        {
            for (int a = 0; a < Userspecified[i]; a++)
            {
                for (int b = 0; b < Userspecified[i-1]; b++)
                {
                    W[i][a][b] = 0;
                }
            }    
        }
    }
}

In the end, given

Userspecified= new int[ ] { 3, 5, 1 };  

If specified "by hand", W should have these dimensions:

W[0]=new double[ ][ ];
W[1]=new double[5][3];
W[2]=new double[1][5];

Obviously, I am doing something wrong since it throws me array out of bounds exceptions. I tried to use http://msdn.microsoft.com/en-us/library/2s05feca.aspx and other questions on SO.

Maybe I am doing this wrong since the beginning though...

Best regards,

Doombot
  • 363
  • 3
  • 6
  • 18
  • For those familiar with the subject, I am trying to initialize the matrices of Weights of a hidden layer perceptron (neural network)... – Doombot Jul 07 '14 at 15:08

3 Answers3

0

I don't think you are traversing your array correctly. Think of it as a jagged array of 2D arrays. Try something more like this, adapted from this answer

foreach (double[,] array in W)
{
   for (int i = 0; i < array.GetLength(0); i++)
   {
       for (int j = 0; j < array.GetLength(1); j++)
       {
           array[i, j] = 0;
       }
   }
}
Community
  • 1
  • 1
tnw
  • 12,391
  • 15
  • 64
  • 108
  • From what I understand, this would do the job, but only if the arrays inside W are already initialized to some dimension. Actually, I think it is my real issue, I am not able to simply create them with the right dimensions in the first place... – Doombot Jul 07 '14 at 15:29
  • @Doombot Ah, I see the problem. Well I can at least explain why you're getting an out of bounds exception. You're trying to traverse an empty array. Arrays are initialized to length 0 if you don't specify otherwise. – tnw Jul 07 '14 at 15:46
  • I have an answer, need to wait 8 more hours to answer my own post since I've got not rep ;) Thanks tnw you helped me a great deal! – Doombot Jul 07 '14 at 18:33
  • @Doombot Happy to help. FYI you can also use lists instead of arrays. Lists don't need to have a predefined length and will grow dynamically as you can simply add items to it rather than defining the length and placing items in particular indices. Often easier than using an array though they both have their use cases. Further reading: http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which – tnw Jul 07 '14 at 18:35
  • Hmm I need to crunch numbers and perform billions of arrays multiplications and divisions, I guess I'm better of with arrays? – Doombot Jul 07 '14 at 19:07
  • @Doombot As the question I linked stated, only if your know *for sure* that your array will be fixed length. You may be trying to optimize a performance issue that doesn't actually exist. My suggestion is to code for readability and maintainability first. – tnw Jul 07 '14 at 19:14
  • You are right! I will remember your advice about optimizing performance issue that doesn't exist, so easy to fall on that path... – Doombot Jul 07 '14 at 19:23
0

Thanks to @tnw for his answers, insights and comments. Here what looks like the code I managed to get done based on it's work, it works for me.

I was unable to solve one issue, though:
I must imply that "neurone" will contain 3 elements, which is 99% of time correct but not flexible.

If anyone has an idea how to solve this...

Anyway, here is the code:

public class PattRec
{
    public int[] neurone;
    private double[][][] W = new double[3][][];

    public PattRec()
    {
        neurone = new int[] { 3, 5, 1 };

        W[0] = new double[][] {new double[1],new double[neurone[1]]};
        W[1] = new double[][] {new double[neurone[1]],new double[neurone[0]] };
        W[2] = new double[][] {new double[neurone[2]],new double [neurone[1]] };

        foreach (double[][] array in W)
        {
            foreach (double[] item in array)
            {
                for (int i = 0; i < item.GetLength(0); i++)
                {
                    item[i] = 0;
                }    
            }

        }
    }

  // Rest of the class, some other methods here, etc.
}
Doombot
  • 363
  • 3
  • 6
  • 18
0

You can use a recursive method. It will, however require you to specify the neuron size(somehow) and the initial value for each element in the array. The neuron size will be the size of the inner array.

this.InitializeJaggedArray(w, 5, 3);
....
private double[][][] w = new double[3][][];
private void InitializeJaggedArray(IList arr, object initializeValue, int neuronSize)
{
    for (int i = 0; i < arr.Count; i++)
    {
        if (arr[i] == null)
        {
            arr[i] = Activator.CreateInstance(arr.GetType().GetElementType(), new object[] { neuronSize } );
        }

        if(arr[i] is IList)
        {
            InitializeJaggedArray(arr[i] as IList, initializeValue, neuronSize);
        }
        else
        {
            arr[i] = initializeValue;
        }
    }
}

This will initialize any array you give to it.

Georgi-it
  • 3,458
  • 1
  • 17
  • 23