【发布时间】: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