简体   繁体   English

如何通过C#在数据集数据表中计算移动平均计算?

[英]How to calculate Moving Average calculations in Dataset Datatable by C#?

Is there any way to do moving average calculations in Dataset Datatable. 有什么方法可以在数据集数据表中进行移动平均计算。 Is this the best option for calculating moving average calculations by using C sharp? 这是使用C Sharp计算移动平均值计算的最佳选择吗? what i am thinking is Datatable looks like excel, because of i am very familier with Excel and vba macros i think this is the best options. 我在想的是Datatable看起来像excel,因为我非常熟悉Excel和vba宏,所以我认为这是最好的选择。 I am just beginer in C sharp i don't know other options, so anyone have any information about it, it will very helpful for me. 我只是C语言的入门者,我不知道其他选择,所以任何人都对此有任何信息,这对我很有帮助。 And thanks in advance for your time. 在此先感谢您的宝贵时间。

Something as simple as this could do the trick. 像这样简单的事情就可以解决问题。

static class Extensions
{
     public static IEnumerable<double> MovingAverage(this double[] numbers, int runs)
     {
         for (var i = 0; i < numbers.Length - runs + 1; i++)
             yield return Enumerable.Range(i, runs).Average(idx => numbers[idx]);
     }
}

Use case: 用例:

 foreach (var number in new[] {1d, 2d, 7d, 4d, 5}.MovingAverage(2))
     Console.WriteLine(number);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM