Jump to content

Array Nightmare - Need solution by morning!!!!!


Mulhoc

Recommended Posts

Hi,

 

I have an assignment due in the morning and I need help. I've written code to do a data comparison using the Manhattan formula.

 

It works fine but I want it to pull out the best match so I can just print that one (it currently prints all comparisons). I've included the main bit of code below. I've added a bit of code that I hoped would keep track of the most similar record and its number but I'm not sure it will work.

 

Does ANYONE know how I can do this?

 

Many, many thanks,

 

Cath

 

//Calculate distance and similarity, going through the array record by record.

for ($i1=0; $i1<$array_count; $i1++) {

  $distance = 0;

  for ($i2=0; $i2<10; $i2++) {

    if ($i2 == 0) {

      $distance = $distance + abs($n_gender - $v0);

    }

if ($i2 == 1) {

      $distance = $distance + abs($n_origin - $v1);

    }

    if ($i2 == 2) {

      $distance = $distance + abs($n_moa - $v2);

    }

    if ($i2 == 3) {

      $distance = $distance + abs($n_year - $v3);

    }

if ($i2 == 4) {

      $distance = $distance + abs($array[$i1][$i2] - $v4);

    }

    if ($i2 == 5) {

      $distance = $distance + abs($array[$i1][$i2] - $v5);

    }

    if ($i2 == 6) {

      $distance = $distance + abs($array[$i1][$i2] - $v6);

    }

    if ($i2 == 7) {

      $distance = $distance + abs($array[$i1][$i2] - $v7);

    }

if ($i2 == 8) {

      $distance = $distance + abs($array[$i1][$i2] - $v8);

    }

    if ($i2 == 9) {

      $distance = $distance + abs($array[$i1][$i2] - $v9);

    }

  }

  $distance = $distance/10;

  $similarity = 1 - $distance;

if($similarity >= $best) {

$best = $similarity;

$record = $i1;

}

}

  echo "The best match is $record with a similarity: $similarity <br><br>" ;

Link to comment
https://forums.phpfreaks.com/topic/48505-array-nightmare-need-solution-by-morning/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.