从数字列表中获取极值 [英] get extremes from list of numbers

查看:33
本文介绍了从数字列表中获取极值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个这样的数字列表:

10,9,8,8,9,7,6,5,4,6,7,8,11,10,12,14,16,20,30,29,28,29,27、25、20、18、15、10、8、5、4、1

我想得到以下数字(根据这个例子):10,4,30,1

是否可以编写一个函数来实现这些极端情况?

这些数字是图表的值,我想获得图表的峰值.

解决方案

我的类似于 jprofitt 的

但是我把它们分成了高峰和低谷,所以我可以用它做更多的事情.

我认为他的循环比我的更干净,但我只是想亲自测试一下.
不要评判我eeeee

这个脚本只是绘制出点并选择峰和谷,并分别给它们绿色和红色.将其视为视觉辅助工具.:P

 $peaks, "valleys" => $valleys, "peak_keys" => $peak_keys, "valley_keys" => $valley_keys);}?><style type="text/css">.容器{位置:绝对;}.观点{位置:绝对;宽度:4px;高度:4px;背景:黑色;}.极端{位置:绝对;顶部:5px;}.extr_low{背景:红色;}.extr_high{背景:绿色;}</风格><?php//阴谋echo "

";foreach($plot as $key => $point){$left = ($key*10);$top = 400 - ($point*10);if(in_array($key, $res['peak_keys']) || in_array($key, $res['valley_keys'])){$extreme = "<div class='extreme'>$point</div>";}别的{$extreme = "";}if(in_array($key, $res['peak_keys'])){$xc = "extr_high";}elseif(in_array($key, $res['valley_keys'])){$xc = "extr_low";}别的{$xc = "";}echo "<div class='point $xc' style='left: ".$left."px; top: ".$top."px;'>$extreme</div>";}echo "</div>";?><表格><tr><th>&nbsp;</th><th>谷</th><th>峰值</th></tr><tr><th>最低</th><td><?php echo min($res['valleys']);?></td><td><?php echo min($res['peaks']);?></td></tr><tr><th>最高</th><td><?php echo max($res['valleys']);?></td><td><?php echo max($res['peaks']);?></td></tr>

If I have a list of numbers like this one:

10,9,8,8,9,7,6,5,4,6,7,8,11,10,12,14,16,20,30,29,28,29,27,25,20,18,15,10,8,5,4,1

I want to get the following numbers (accordint to this example) : 10,4,30,1

Is it possible to write a function which will get these extremes?

These numbers are values of a chart, I want to get peaks of the chart.

解决方案

Mine's similar to jprofitt's

but I divided them into peaks and valleys, so I can do some more stuff with it.

I think his loop is much more cleaner than mine is, but I just wanted to test it out for myself.
Don't judge meeeeee

This script just plots the points out and selects the peaks and valleys and give them green and red respectively. See it as a visual aid. :P

<?php

$plot = array(10,9,8,8,9,7,6,5,4,6,7,8,11,10,12,14,16,20,30,29,28,29,27,25,20,18,15,10,8,5,4,1);

$res = local_extremes($plot);

function local_extremes(array $array){
    $peaks = array();
    $valleys = array();

    $peak_keys = array();
    $valley_keys = array();

    for($i = 0; $i < count($array); $i++){
        $more_than_last = $array[$i] > $array[$i-1];
        $more_than_next = $array[$i] > $array[$i+1];

        $next_is_equal = $array[$i] == $array[$i+1];

        if($next_is_equal) continue;

        if($i == 0){
            if($more_than_next){
                $peaks[] = $array[$i];
                $peak_keys[] = $i;
            }else{
                $valleys[] = $array[$i];
                $valley_keys[] = $i;
            }
        }elseif($i == (count($array)-1)){
            if($more_than_last){
                $peaks[] = $array[$i];
                $peak_keys[] = $i;
            }else{
                $valleys[] = $array[$i];
                $valley_keys[] = $i;
            }
        }else{
            if($more_than_last && $more_than_next){
                $peaks[] = $array[$i];
                $peak_keys[] = $i;
            }elseif(!$more_than_last && !$more_than_next){
                $valleys[] = $array[$i];
                $valley_keys[] = $i;
            }
        }
    }

    return array("peaks" => $peaks, "valleys" => $valleys, "peak_keys" => $peak_keys, "valley_keys" => $valley_keys);
}
?>

<style type="text/css">
    .container{
        position: absolute;
    }

    .point{
        position: absolute;
        width: 4px;
        height: 4px;
        background: black;
    }

    .extreme{
        position: absolute;
        top: 5px;
    }

    .extr_low{
        background: red;
    }

    .extr_high{
        background: green;
    }
</style>

<?php

//Plot
echo "<div class='container'>";
foreach($plot as $key => $point){
    $left = ($key*10);
    $top = 400 - ($point*10);

    if(in_array($key, $res['peak_keys']) || in_array($key, $res['valley_keys'])){
        $extreme = "<div class='extreme'>$point</div>";
    }else{
        $extreme = "";
    }

    if(in_array($key, $res['peak_keys'])){
        $xc = "extr_high";
    }elseif(in_array($key, $res['valley_keys'])){
        $xc = "extr_low";
    }else{
        $xc = "";
    }

    echo "<div class='point $xc' style='left: ".$left."px; top: ".$top."px;'>$extreme</div>";
}
echo "</div>";

?>

<table>
    <tr>
        <th>&nbsp;</th>
        <th>Valley</th>
        <th>Peak</th>
    </tr>
    <tr>
        <th>Lowest</th>

        <td><?php echo min($res['valleys']); ?></td>
        <td><?php echo min($res['peaks']); ?></td>
    </tr>
    <tr>
        <th>Highest</th>
        <td><?php echo max($res['valleys']); ?></td>
        <td><?php echo max($res['peaks']); ?></td>
    </tr>
</table>

这篇关于从数字列表中获取极值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆