0

我有一个由 4 个双列组成的列表

class MainE
{
     class cPoint
    {
        public double a;
        public double b;
        public double c;
        public double d;
    };

static void Main()
{
    List<cPoint> lst = new List<cPoint>();
    lst .Add(new cPoint(){a =613, b = 261, c=163,d=345 });
    lst .Add(new cPoint(){a =271, b = 251, c=363,d=444 });
    lst .Add(new cPoint(){a =181, b = 232, c=473,d=643 });
    lst .Add(new cPoint(){a =411, b = 322, c=643,d=742 });
    lst .Add(new cPoint(){a =542, b = 225, c=853,d=141 });
    lst .Add(new cPoint(){a =661, b = 242, c=293,d=241 });
    lst .Add(new cPoint(){a =771, b = 232, c=143,d=243 });
    lst .Add(new cPoint(){a =481, b = 212, c=353,d=444 });
    lst .Add(new cPoint(){a =681, b = 214, c=233,d=514 });
    lst .Add(new cPoint(){a =613, b = 241, c=123,d=355 });
    lst .Add(new cPoint(){a =271, b = 451, c=363,d=444 });
    lst .Add(new cPoint(){a =171, b = 232, c=463,d=743 });
    lst .Add(new cPoint(){a =419, b = 362, c=653,d=782 });
    lst .Add(new cPoint(){a =142, b = 227, c=853,d=149 });
    lst .Add(new cPoint(){a =661, b = 282, c=943,d=241 });
    lst .Add(new cPoint(){a =721, b = 282, c=444,d=343 });
    lst .Add(new cPoint(){a =482, b = 292, c=323,d=424 });
    lst .Add(new cPoint(){a =641, b = 219, c=123,d=514 });

}

}

我想从应用公式的原始列表创建 2 个列表(此处为向量编写):

一个列表需要将以下公式应用于原始列表中的每一列

for (i = 1; i < N - 1; ++i)
{
    output[i] = (input[i - 1] + input[i] + input[i + 1]) / 3;
}

第二个列表需要将以下公式应用于原始列表中的每一列

for (i = 1; i < N ; ++i)
{
    output[i] = input[i] - input[i - 1];
}

这里的问题是我不知道如何获取列表中的上一个元素和下一个元素,以及如何从列表中的第二个元素开始执行它并在最后一个元素之前完成......

如何将每个公式应用于原始列表中的每一列?

4

2 回答 2

2

indexer您可以为您的cPoint班级定义一个:

class cPoint {
    static cPoint(){
      //We just care about the Fields of type double
      fields = typeof(cPoint).GetFields().Where(f=>f.FieldType==typeof(double)).ToArray();
      ColumnCount = fields.Length;
    }
    static FieldInfo[] fields;
    public static int ColumnCount {get;private set;}
    public double a;
    public double b;
    public double c;
    public double d;
    public double this[int index]{
       get {
          return (double)fields[index].GetValue(this);
       }
       set {
          fields[index].SetValue(this, value);
       }
    }
}

var list1 = lst.Select(x=> {                    
                cPoint output = new cPoint();
                output[0] = x[0];
                for(int i = 1; i < x.ColumnCount-1; i++){
                   output[i] = (x[i - 1] + x[i] + x[i + 1]) / 3;
                }
                return output;
            }).ToList();
var list2 = lst.Select(x=> {
                cPoint output = new cPoint();
                output[0] = x[0];
                for(int i = 1; i < x.ColumnCount; i++){
                   output[i] = x[i] - x[i - 1];
                }
                return output;
            }).ToList();

注意:您应该定义一些internal list以按顺序公开您Columns的内容,因为使用Reflection这种方式可能会降低性能。然而,使用这里的好处是您可以轻松地添加或删除字段,只需按照您想要的顺序定义字段,您定义字段的顺序正是您想要在 for 循环中循环的列的顺序Reflection

于 2013-09-20T08:16:35.650 回答
1

放置索引器的替代方法cPoint

    class cPoint
    {
        private readonly double[] values = new double[4];

        public double this[int index] {
            get { return values[index]; }
            set { values[index] = value; }
        }

        public int Length
        {
            get { return values.Length; }
        }

        public double a
        {
            get { return this[0]; }
            set { this[0] = value; }
        }
        public double b
        {
            get { return this[1]; }
            set { this[1] = value; }
        }
        public double c
        {
            get { return this[2]; }
            set { this[2] = value; }
        }
        public double d
        {
            get { return this[3]; }
            set { this[3] = value; }
        }
    };
于 2013-09-20T08:37:20.817 回答