日期:2014-05-18  浏览次数:20865 次

数组定义问题
大家好:
  问题是这样的:我想编一个K均值的聚类算法,如何定义这样的一个数组,它包括19个记录,但每个记录又由4个变量组成,见下面数据。  
  month X1 X2 X3 X4
  1 11.03 50.30 11.81 11.27
  2 5.47 19.30 5.20 7.18
  3 3.58 9.85 3.14 2.11
  4 2.01 4.17 1.47 1.58
  6 2.13 5.65 1.04 2.11
  8 2.06 1.74 .17 1.57
  10 1.63 2.04 1.04 1.46
  12 1.17 1.60 .89 .76
  15 1.03 2.34 .53 .89
  18 .69 1.33 .48 .58
  24 .77 1.41 .52 .42
  30 .59 1.25 .30 .14
  36 .65 1.19 .49 .38
  42 .51 .93 .16 .25
  48 .73 1.13 .35 .55
  54 .53 .82 .16 .34
  60 .36 .52 .19 .21
  66 .52 1.03 .30 .55
  72 .34 .49 .18 .16

请高手提点一下,把里面的变量能不能定义成结构呀?。。。。。。在线等

------解决方案--------------------
I suggest you to define an arrylist object, and then you can use it's Add method to add your object before you create a object .

ArrayList list = new ArrayList();
list.Add(yourobject);


you must define the special class/struct for you data model.
------解决方案--------------------
public struct struct1
{
public double f1;
public double f2;
public double f3;
public double f4;
}
 struct1[] A = new struct1[19];
 A[i].f1 = XXX;
 A[i].f2 = XXX;
 A[i].f3 = XXX;
 A[i].f4 = XXX;
------解决方案--------------------
C# code

struct data
{
    public double month;
    public double X1;
    public double X2;
    public double X3;
    public double X4;
    public data(double month, double x1, double x2, double x3, double x4)
    {
        this.month = month;
        this.X1 = x1;
        this.X2 = x2;
        this.X3 = x3;
        this.X4 = x4;
    }
}
private void abc()
{
    List<data> list = new List<data>();
    list.Add(new data(1, 11.03, 50.30, 11.81, 11.27));
    list.Add(new data(2, 5.47, 19.30, 5.20, 7.18));
    list.Add(new data(3, 3.58, 9.85, 3.14, 2.11));
    list.Add(new data(4, 2.01, 4.17, 1.47, 1.58));
    list.Add(new data(6, 2.13, 5.65, 1.04, 2.11));
    list.Add(new data(8, 2.06, 1.74, 0.17, 1.57));
    list.Add(new data(10, 1.63, 2.04, 1.04, 1.46));
    list.Add(new data(12, 1.17, 1.60, 0.89, 0.76));
    list.Add(new data(15, 1.03, 2.34, 0.53, 0.89));
    list.Add(new data(18, 0.69, 1.33, 0.48, 0.58));
    list.Add(new data(24, 0.77, 1.41, 0.52, 0.42));
    list.Add(new data(30, 0.59, 1.25, 0.30, 0.14));
    list.Add(new data(36, 0.65, 1.19, 0.49, 0.38));
    list.Add(new data(42, 0.51, 0.93, 0.16, 0.25));
    list.Add(new data(48, 0.73, 1.13, 0.35, 0.55));
    list.Add(new data(54, 0.53, 0.82, 0.16, 0.34));
    list.Add(new data(60, 0.36, 0.52, 0.19, 0.21));
    list.Add(new data(66, 0.52, 1.03, 0.30, 0.55));
    list.Add(new data(72, 0.34, 0.49, 0.18, 0.16));
}