douzi1991 2010-06-22 08:05
浏览 83
已采纳

如何轻松优雅地进行PHP矩阵操作?

I've a $max which is essentially a two dimensional array.

Each element in $max is eithor 1 or 0,

can be denoted by $max[$x][$y], where $x is an integer within 0~WIDTH,similar for $y

My purpose is to find rows and columns in the $maxthat sums up greater than a CONSTANT, and get the average distance between rows/columns that qualify.

Anyone has a good solution ?

  • 写回答

1条回答 默认 最新

  • duanlu4371 2010-06-22 08:25
    关注

    I have not tested this, but it should work for summing up the columns and rows:

    //Map columns and rows into their respective values
    //Note that we preserve the col/row indexes
    $rowval = array();
    $colval = array();
    foreach($max as $k1 => $row) {
      $rowval[$k1] = array_sum($row);
      foreach($row as $k2 => $col) {
        if(!isset($colval[$k2])) {
          $colval[$k2] = 0;
        }
        $colval[$k2] += $col;
      }
    }
    
    //Define filter function
    function is_over($val) {
      return $val > CONSTANT;
    }
    
    //Filter out the cols/rows from their respective arrays
    //Keys will be preserved by array_filter
    $foundcols = array_filter($colval, 'is_over');
    $foundrows = array_filter($rowval, 'is_over');
    

    You still have to calculate the average distance though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置