Mulhoc Posted April 24, 2007 Share Posted April 24, 2007 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 == { $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>" ; Quote Link to comment https://forums.phpfreaks.com/topic/48505-array-nightmare-need-solution-by-morning/ Share on other sites More sharing options...
taith Posted April 24, 2007 Share Posted April 24, 2007 do yourself a HUGE favor... a) switch from if()'s to switch()... b) then you use break 2;... so it finds the best solution, then skips out of your for() Quote Link to comment https://forums.phpfreaks.com/topic/48505-array-nightmare-need-solution-by-morning/#findComment-237258 Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 AND wrap your code on [ code][/ code] tags so it makes it easier for people to read, thanks Quote Link to comment https://forums.phpfreaks.com/topic/48505-array-nightmare-need-solution-by-morning/#findComment-237260 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 PS we have a freelance section if its that important! Quote Link to comment https://forums.phpfreaks.com/topic/48505-array-nightmare-need-solution-by-morning/#findComment-237263 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.