c# - 从值列表中推断出下降趋势

标签 c# algorithm

在我的程序中每秒调用的函数中,我得到一个表示一些 X 强度的浮点值。
这些值不断出现,我希望存储最近 30 个值的历史记录,并检查这些值是否存在下降/下降趋势(也可能有 2 或 3 个误报,因此必须忽略这些值).
如果出现下降趋势并且(如果最新值 减去 历史记录中的第一个值)超过阈值 50(比方说),我想调用另一个函数。这样的东西怎么能在C#中实现呢,C#有这样一个结构,可以存储30个值的历史,然后分析/推导出下降趋势?

最佳答案

您有多种选择。如果你只需要每秒调用一次,你可以使用 Queue<float> ,像这样:

Queue<float> theQueue = new Queue<float>(30);

// once per second:
// if the queue is full, remove an item
if (theQueue.Count >= 30)
{
    theQueue.Dequeue();
}
// add the new item to the queue
theQueue.Enqueue(newValue);

// now analyze the items in the queue to detect a downward trend
foreach (float f in theQueue)
{
    // do your analysis
}

这很容易实现,而且速度足以每秒运行一次。

如何分析下降趋势实际上取决于您的定义。

我突然想到 Queue<float>枚举器可能无法保证按插入顺序返回内容。如果没有,那么您将不得不实现自己的 circular buffer .

关于c# - 从值列表中推断出下降趋势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18425593/

相关文章:

c# - XML 配置中的变量

c# - 更改 PrinterSettings.Copies 不起作用

algorithm - 重力排序 : Is this possible programmatically?

algorithm - 网格图论

algorithm - 如何调整 Fenwick 树以回答范围最小查询

c# - 在另一个列表中添加列表项

C# 面向对象 : Using interface with type parameters in base concrete class

C# 随机数生成器每次仍然给出相同的数字

algorithm - 如何在 PI 的小数位内找到一个特定的序列?

python - 从偏好列表中找到可行的组合