0

I am implementing k- nearest neighbor alog using euclidian distance formula for 2-D data in my sql table. Things look ok below even it shows strange behavious.

Code:

    $result1 = mysqli_query($con,"SELECT pcount, ncount from test");
    $result2 = mysqli_query($con,"SELECT pcount, ncount from test");
    $i = 0;
    $min = 0;
    while ($row1 = @mysqli_fetch_array($result1))
    {
        $pcount = $row1['pcount'];
        $ncount = $row1['ncount'];
        echo "pcount is $pcount<br/>";
        echo "ncount is $ncount<br/></br>";
        $a[$i] = $pcount ;
        $b[$i] = $pcount ;
        $j = 0;
        while ($row2 = @mysqli_fetch_array($result2))
        {
            echo "j is $j <br/>";
            $a[$j] = $row2['pcount'];   
            $b[$j] = $row2['ncount'];
            $diff = sqrt(($a[$i] - $a[$j])^2 + ($b[$i] - $b[$j])^2 );
        $j= $j + 1;
        echo "$diff <br>";
        }               
        echo "i is $i <br/>";
                $i = $i + 1;    
    }

it gives this result:

j is 0 
Difference : 0 
j is 1 
Difference : 1.4142135623731 
j is 2 
Difference : 0 
j is 3 
Difference : 0 
j is 4 
Difference : 1.4142135623731 
j is 5 
Difference : 1.4142135623731 
j is 6 
Difference : 1.4142135623731 
i is 0 

In table I have total 8 rows, but above result is for 7 rows only. Why?

And First while loop gets executed for $i = 0 on, for rest i=1 to 7 no operation. Can some one tell me what is bug?

data in table is

pcount  ncount
20  80
21  79
20  80
21  79
19  81
20  80
20  80
20  80

result after fallens answer:

j is 0 
Difference : 0 
j is 1 
Difference : NAN 
j is 2 
Difference : 0 
j is 3 
Difference : NAN 
j is 4 
Difference : 1.4142135623731 
j is 5 
Difference : 0 
j is 6 
Difference : 0 
j is 7 
Difference : 0 
i is 0 
pcount is 21
ncount is 79

i is 1 
pcount is 20
ncount is 80

i is 2 
pcount is 21
ncount is 79

i is 3 
pcount is 19
ncount is 81

i is 4 
pcount is 20
ncount is 80

i is 5 
pcount is 20
ncount is 80

i is 6 
pcount is 20
ncount is 80

i is 7 
3
  • Could it fail because you used the same variable in both while-loops? Try replacing the $row in the second loop with something like $row2. Commented Feb 20, 2014 at 13:09
  • @mnme: thanks but no changes Commented Feb 20, 2014 at 13:11
  • why dont you print j on second while? it can give a clue Commented Feb 20, 2014 at 13:17

1 Answer 1

1

You are using same variable in inner and outer loop. Try different variable names for rows and results in these two loops. Like result1, row1 for outer and result2, row2 for inner loop

It shows 7 rows instead of 8, because first row from result is picked by outer loop already

Try this piece of code:

$result1 = mysqli_query($con,"SELECT pcount, ncount from test");
$i = 0;
$min = 0;
while ($row1 = @mysqli_fetch_array($result1))
{
    $pcount = $row1['pcount'];
    $ncount = $row1['ncount'];
    echo "pcount is $pcount<br/>";
    echo "ncount is $ncount<br/></br>";
    $a[$i] = $pcount ;
    $b[$i] = $pcount ;
    $j = 0;

    $result2 = mysqli_query($con,"SELECT pcount, ncount from test");

    while ($row2 = @mysqli_fetch_array($result2))
    {
        echo "j is $j <br/>";
        $a[$j] = $row2['pcount'];   
        $b[$j] = $row2['ncount'];
        $diff = ($a[$i] - $a[$j])^2 + ($b[$i] - $b[$j])^2;
        if( $diff != 0)
        $diff = sqrt( $diff );
    $j= $j + 1;
    echo "$diff <br>";
    }               
    echo "i is $i <br/>";
            $i = $i + 1;    
}

Note To avoid NaN, make sure ($a[$i] - $a[$j])^2 + ($b[$i] - $b[$j])^2 doesn't yield 0.

Sign up to request clarification or add additional context in comments.

3 Comments

@Programming_crazy: I've added code. Please note that instead of initializing $result2 once, this updated code initialize it for every possible outer loop
can you please suggest me to encount ($a[$i] - $a[$j])^2 + ($b[$i] - $b[$j])^2, bcoz my data has frequent value where it yields 0
If you check the updated code, you can see this part $diff = ($a[$i] - $a[$j])^2 + ($b[$i] - $b[$j])^2; if( $diff != 0) $diff = sqrt( $diff ); filters $diff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.