Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从列数据中查找峰值间隔_C#_Math - Fatal编程技术网

C# 从列数据中查找峰值间隔

C# 从列数据中查找峰值间隔,c#,math,C#,Math,我有一些整数值,如下所示: 2 464 2 484 2 374 2 我需要找到峰值,也就是数组中值停止增加的点。在上面的示例中,有三个峰值-6、8、7 怎样才能做到呢?谢谢。我不知道你想要的到底是什么,但以下是我得到的: public List<Tuple<int, int>> GetPeaks(int[] values) { List<Tuple<int, int>> results = new List<Tuple<int,

我有一些整数值,如下所示:

2 464 2 484 2 374 2

我需要找到峰值,也就是数组中值停止增加的点。在上面的示例中,有三个峰值-
6、8、7


怎样才能做到呢?谢谢。

我不知道你想要的到底是什么,但以下是我得到的:

public List<Tuple<int, int>> GetPeaks(int[] values)
{
    List<Tuple<int, int>> results = new List<Tuple<int, int>>();

    List<int> curInterval = new List<int>();

    bool decreasing = false;
    for (int i = 0; i < values.Length; i++)
    {
        if (curInterval.Count > 0)
        {
            if (values[i] < curInterval.Last() && !decreasing)
            {
                results.Add(new Tuple<int, int>(i - 1, curInterval.Last()));
                curInterval.Clear();
                decreasing = true;
            }
            else if (values[i] >= curInterval.Last() && decreasing)
            {
                decreasing = false;
            }
        }
        curInterval.Add(values[i]);
    }
    return results;
}
public List GetPeaks(int[]值)
{
列表结果=新列表();
List curInterval=新列表();
布尔递减=假;
for(int i=0;i0)
{
if(值[i]=curInterval.Last()&递减)
{
递减=假;
}
}
添加(值[i]);
}
返回结果;
}

似乎有效(编辑后更新)。该方法将返回包含(peakPosition,peakValue)的元组。代码非常简单,我确信可以做得更好。

我不知道您想要得到的到底是什么,但以下是我得到的:

public List<Tuple<int, int>> GetPeaks(int[] values)
{
    List<Tuple<int, int>> results = new List<Tuple<int, int>>();

    List<int> curInterval = new List<int>();

    bool decreasing = false;
    for (int i = 0; i < values.Length; i++)
    {
        if (curInterval.Count > 0)
        {
            if (values[i] < curInterval.Last() && !decreasing)
            {
                results.Add(new Tuple<int, int>(i - 1, curInterval.Last()));
                curInterval.Clear();
                decreasing = true;
            }
            else if (values[i] >= curInterval.Last() && decreasing)
            {
                decreasing = false;
            }
        }
        curInterval.Add(values[i]);
    }
    return results;
}
public List GetPeaks(int[]值)
{
列表结果=新列表();
List curInterval=新列表();
布尔递减=假;
for(int i=0;i0)
{
if(值[i]=curInterval.Last()&递减)
{
递减=假;
}
}
添加(值[i]);
}
返回结果;
}


似乎有效(编辑后更新)。该方法将返回包含(peakPosition,peakValue)的元组。代码非常简单,我确信可以做得更好。

如何定义峰值?@st4hoo应该是值开始减少的点。正如我在问题中所描述的,6是峰值,然后4,2,4更小,然后8是峰值。这样,如果你试图把它想象成条形图,那么我想考虑两个峰值之间的间隔。i、 e在6和8之间,8和7之间,间隔就是两个峰值之间的数值。间隔是指6到8之间有多少个数字。所以在6和8之间有3个数字,所以间隔是3。如果有这样的序列呢:123245231。3的首次出现也是一个峰值吗?如果:12344231怎么办?在这种情况下,我们是有三个峰值还是只有一个峰值?你如何定义峰值?@st4hoo应该是一个值开始减少的点。正如我在问题中所描述的,6是峰值,然后4,2,4更小,然后8是峰值。这样,如果你试图把它想象成条形图,那么我想考虑两个峰值之间的间隔。i、 e在6和8之间,8和7之间,间隔就是两个峰值之间的数值。间隔是指6到8之间有多少个数字。所以在6和8之间有3个数字,所以间隔是3。如果有这样的序列呢:123245231。3的首次出现也是一个峰值吗?如果:12344231怎么办?在这种情况下,我们是有三个峰还是只有一个峰?这是我的荣幸:pi用931137550941271186362111129105497712492489813713786011546657812915011358971461466211343731341621296314811211316015290731321674891501676711016116410569128进行了试验168 155 82 89 146 170 137 74 107 160 171 121 72 123 171 168 105 82 144 173 155 82 100 160 176 139 78 120 168 172 114 78 136 173 162 97 157 182 146 79 110 162 176 126 75 129 177 167 103 85 147 175 152 86 158 174 132 75 117 166 170 112 74 172 155 84 92 151 171 140 74 105 160但它没有如预期的那样工作是的,这只是一个逻辑上的小漏洞,一秒钟。编辑:完成,立即尝试是的,现在它似乎给出了预期的结果。否则,如果(curInterval.Count>0&&values[i]>=curInterval.Last()&&discreating){discreating=false;}需要检查计数>0,否则将抛出错误。谢谢你的帮助。(非常感谢。)问题是,我的循环自动地说,在每一个新的间隔检查第一个值之后,值不再减少。将“else”更改为“else if”我很高兴:pi用9311375509412711863621112197471329248981371378601114612465781291135897146 94621137143731621626331481126111416090132162717171717171648181816881558289试过了146 170 137 74 107 160 171 121 72 123 171 168 105 82 144 173 155 82 100 160 176 139 78 120 168 172 114 78 136 173 162 97 95 157 182 146 79 110 162 176 126 75 129 177 167 103 85 147 175 152 86 103 158 174 132 75 117 166 170 112 74 134 172 155 84 92 151 171 140 74 105 160但它没有按预期工作是的,这里只是一个小的逻辑缺陷,一秒钟。编辑:完成,立即尝试是的,现在它似乎给出了预期的结果。否则,如果(curInterval.Count>0&&values[i]>=curInterval.Last()&&discreating){discreating=false;}需要检查计数>0,否则将抛出错误。谢谢你的帮助。(非常感谢。)问题是,我的循环自动地说,在测试结束后,值不再减少