tags:

views:

38

answers:

1

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 ?

A: 

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.

Emil Vikström
@Emil,thanks, almost working!
wamp