Results 1 to 9 of 9
  1. #1
    nvwin is offline Member
    Join Date
    May 2016
    Posts
    6
    Rep Power
    0

    Default Different results from euclidean distance program

    Hi,


    I calculate the euclidean distance for two vector arr1, arr2 , then calculate it for X which is X={{arr1} ,{arr2}}.
    iam receiving different results:

    the code is :


    import java.util.*;

    class test22{

    public static void main ( String [] args ) {


    double [] array1= {1.2,2.5,4.7,5.5};
    double [] array2= {5.5,4.6, 6.8, 7.8};

    double [][] X = {{1.2,2.5,4.7,5.5},{5.5,4.6, 6.8, 7.8}};


    test22 test = new test22();

    double Dist1 = test.calculateDistance( array1,array2);

    double Dist2 = test.Euclideanorm(X);



    System.out.println(Dist1);

    System.out.println(Dist2);


    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    static public double Euclideanorm(double[][] X){


    double sum = 0.0;
    for(int i=0; i<X.length; i++) {
    for(int j=0; j<X[0].length; j++) {
    sum += X[i][j];
    }
    }
    return Math.sqrt(sum);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public static double calculateDistance(double[] array1, double[] array2)
    {
    double Sum = 0.0;

    for (int i=0;i<array1.length;i++) {
    Sum = Sum+ Math.pow((array1[i]-array2[i]),2.0);
    }

    return Math.sqrt(Sum);
    }


    the result :

    5.7

    6.2 ????


    }
    Last edited by nvwin; 05-14-2016 at 05:51 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Different results from euclidean distance program

    At least one problem is that in your calculate distance method you are not summing, only assigning and overwriting.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    nvwin is offline Member
    Join Date
    May 2016
    Posts
    6
    Rep Power
    0

    Default Re: Different results from euclidean distance program

    @ Jim , i fix the bug which you point to. but still have different results :(
    Last edited by nvwin; 05-14-2016 at 05:52 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Different results from euclidean distance program

    but still have different results
    Which answer is correct? That will show you which method is doing a bad computation.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    nvwin is offline Member
    Join Date
    May 2016
    Posts
    6
    Rep Power
    0

    Default Re: Different results from euclidean distance program

    @ Norm : the Euclideanorm method result is 6.21 , and the other one is 5.7 .

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Different results from euclidean distance program

    Ok. Are both of those results correct? If any of the results are incorrect, which method(s) returns the wrong result?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Different results from euclidean distance program

    In one case, you are summing the square of the differences of array elements. Then taking the square root.
    In the other case, you are simply adding up all the array elements and then taking the square root. These are not
    equivalent operations.


    A1 = {1,2}
    A2 = {3,4}
    B = {1,2},{3,4}

    (1 + 2 + 3 + 4)^.5 = sq root of 10.
    ((1-3)^2 + (2-4)^2)^.5 == sq root of 8.

    So there are three possibilities. 1) they are not supposed to be equal, 2) the algorithms are wrong, 3) or I am wrong.

    Regards,
    Jim
    Last edited by jim829; 05-14-2016 at 06:50 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    nvwin is offline Member
    Join Date
    May 2016
    Posts
    6
    Rep Power
    0

    Default Re: Different results from euclidean distance program

    Yes The second calculation is different ,

    but how the euclidean distance calculate for 2D array ??

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Different results from euclidean distance program

    t how the euclidean distance calculate for 2D array
    Do you have a textbook that explains how to do the calculation? What are the steps or algorithm that does the calculation?
    You need the algorithm that does the calculation before you can write the code to implement it.

    Here's a way to use one of the given methods with a 2 dim array:
    Java Code:
    		  System.out.println(calculateDistance(X[0], X[1]));
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: 09-27-2013, 09:33 AM
  2. Replies: 6
    Last Post: 09-27-2013, 09:33 AM
  3. diving distance program
    By maximumammo in forum New To Java
    Replies: 4
    Last Post: 04-05-2012, 08:09 PM
  4. Replies: 1
    Last Post: 11-17-2011, 06:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •