# Thread: Different results from euclidean distance program

1. Member
Join Date
May 2016
Posts
6
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## 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

3. Member
Join Date
May 2016
Posts
6
Rep Power
0

## 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. ## 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.

5. Member
Join Date
May 2016
Posts
6
Rep Power
0

## Re: Different results from euclidean distance program

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

6. ## 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?

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

## 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.

8. Member
Join Date
May 2016
Posts
6
Rep Power
0

## Re: Different results from euclidean distance program

Yes The second calculation is different ,

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

9. ## 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]));`