PHP - 根据范围查找数组数据(高和低)中的转折点

标签 php arrays analysis

$data = array(5,0,15,20,22,14,13,15,12,22,40,25);

enter image description here

你好,我想遍历上面的数据点,根据范围找到转折点。

到目前为止,我处理它的方法只是采用 $array[$i] - $array[$i-1] ,如果绝对差值大于范围 - 我将其视为转折点 。然而 - 这个逻辑是有缺陷的,好像它稍微向上移动然后又向下移动 - 它打破了循环。

enter image description here

3 个向下的值应该足以使 X 成为向下的转折点,但因为它们各自不符合范围 - 它们被丢弃。

有什么解决办法吗?

if($diff >= 0)
{
  $diff_up = $diff_up + $diff;
}
else
{
  $diff_down = $diff_down + abs($diff);
}


if((($diff_up-$diff_down) >=$range) && ($pivot_type != "UP"))
{
  echo "Pivot UP at : ".$current;
  break;
}
else if((($diff_down-$diff_up) >$range) && ($pivot_type != "DOWN"))
{
  echo "Pivot DOWN at : ".$current;
  break;
}

最佳答案

你要找的是所有局部最小值和最大值,This is a good article .

我做了这个(灵感来自: get extremes from list of numbers ):

<?php
$data = array(5,0,15,20,22,14,13,15,12,22,40,25);

function minima_and_maxima(array $array){
  $maxima = [];
  $minima = [];

  $maxima[] = $array[0];
  for($i = 1; $i < count($array) - 1; $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) {
        $maxima[] = $array[$i];
      } else {
        $minima[] = $array[$i];
      }
    } elseif ($i == count($array)-1) {
      if ($more_than_last) {
        $maxima[] = $array[$i];
      } else {
        $minima[] = $array[$i];
      }
    } else {
      if ($more_than_last && $more_than_next) {
        $maxima[] = $array[$i];
      } elseif (!$more_than_last && !$more_than_next) {
        $minima[] = $array[$i];
      }
    }
  }

  for ($i = 0; $i < count($maxima); $i++) {
    $current_maxima = $maxima[$i];
    $next_maxima    = $maxima[$i+1];

    if ($current_maxima > $next_maxima) {
      unset($maxima[$i+1]);
    }
  }

  for ($i = 0; $i < count($minima); $i++) {
    $current_minima = $minima[$i];
    $next_minima    = $minima[$i+1];

    if ($next_minima < $current_minima) {
      unset($minima[$i]);
    }
  }

  return [
    'maxima' => array_values($maxima),
    'minima' => array_values($minima),
  ];
}

function get_turning_points($data)
{
  $mins_and_maxs = minima_and_maxima($data);

  $turning_points = [];
  for ($i = 0; $i < count($mins_and_maxs['maxima']) - 1; $i++) {
    $turning_points[] = $mins_and_maxs['maxima'][$i];
    $turning_points[] = $mins_and_maxs['minima'][$i];
  }
  $turning_points[] = $mins_and_maxs['maxima'][count($mins_and_maxs['maxima'])-1];

  return $turning_points;
}

print_r(get_turning_points($data));

这给你:

Array
(
    [0] => 5
    [1] => 0
    [2] => 22
    [3] => 12
    [4] => 40
)

演示:https://eval.in/832708

希望这有帮助:)

关于PHP - 根据范围查找数组数据(高和低)中的转折点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45103962/

相关文章:

javascript - 使用与 Lodash 的差异

algorithm - 使用计数排序算法的一部分返回 a 和 b 之间的元素数

arrays - 找出比较两个项目的概率。 (请提示)

php - 在 PHP 中四舍五入以达到 100%

php - 在 PHPExcel 中加粗单元格使其处于选中状态

php - 我正在应用此 sql 压缩查询,但收到语法错误

php - Yii2:在插入数据库时​​使用mysql AES_ENCRYPT

javascript - 复选框将其值推送到空数组中,但在 jQuery 中未选中时弹出或移动值

c - 普通数组也是动态的吗?

uml - UML类模型如何建立多对多关系