【问题标题】:Initialize jagged arrays of different length automatically自动初始化不同长度的锯齿状数组
【发布时间】:2014-07-07 14:54:24
【问题描述】:

我正在尝试在类的构造函数中将锯齿状数组的元素值初始化为 0。虽然我能够指定其他变量的值,但我无法为这个锯齿状数组这样做。

问题在于锯齿状数组的维度取决于另一个数组的值和属性。这就是我所做的,之后是我试图获得的结果。

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;
                }
            }    
        }
    }
}

最后,给定

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

如果“手动”指定,W 应具有以下尺寸:

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

显然,我做错了什么,因为它使我的数组超出范围异常。我尝试在 SO 上使用http://msdn.microsoft.com/en-us/library/2s05feca.aspx 和其他问题。

也许我从一开始就做错了......

最好的问候,

【问题讨论】:

  • 对于熟悉该主题的人,我正在尝试初始化隐藏层感知器(神经网络)的权重矩阵...

标签: c# constructor jagged-arrays


【解决方案1】:

我认为您没有正确遍历数组。将其视为二维数组的锯齿状数组。试试类似的东西,改编自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;
       }
   }
}

【讨论】:

  • 据我了解,这可以完成这项工作,但前提是 W 中的数组已经初始化为某个维度。实际上,我认为这是我真正的问题,我一开始就无法简单地创建具有正确尺寸的它们......
  • @Doombot 啊,我明白了问题所在。好吧,我至少可以解释为什么您会出现越界异常。您正在尝试遍历一个空数组。如果您没有另外指定,则数组初始化为长度 0。
  • 我有一个答案,需要再等 8 个小时才能回答我自己的帖子,因为我没有代表;)谢谢你帮了我很多!
  • @Doombot 乐于助人。仅供参考,您也可以使用列表而不是数组。列表不需要具有预定义的长度,并且会动态增长,因为您可以简单地向其中添加项目,而不是定义长度并将项目放置在特定的索引中。尽管它们都有自己的用例,但通常比使用数组更容易。延伸阅读:stackoverflow.com/questions/434761/…
  • 嗯,我需要处理数字并执行数十亿个数组的乘法和除法,我想我更擅长使用数组?
【解决方案2】:

您可以使用递归方法。但是,它将要求您指定神经元大小(以某种方式)和数组中每个元素的初始值。神经元大小将是内部数组的大小。

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;
        }
    }
}

这将初始化你给它的任何数组。

【讨论】:

    【解决方案3】:

    感谢 @tnw 的回答、见解和 cmets。这里看起来像是我根据它的工作设法完成的代码,它对我有用。

    不过,我无法解决一个问题:
    我必须暗示“神经元”将包含 3 个元素,这是 99% 的时间正确但不灵活。

    如果有人知道如何解决这个问题...

    不管怎样,代码如下:

    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.
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 2019-06-17
      • 1970-01-01
      • 2014-05-03
      • 2011-08-21
      相关资源
      最近更新 更多