Results 1 to 9 of 9
 05142016, 01:23 PM #1Member
 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; 05142016 at 05:51 PM.
 05142016, 02:55 PM #2Senior 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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 05142016, 03:50 PM #3Member
 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; 05142016 at 05:52 PM.
 05142016, 04:29 PM #4
Re: Different results from euclidean distance program
but still have different resultsIf you don't understand my response, don't ignore it, ask a question.
 05142016, 05:50 PM #5Member
 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 .
 05142016, 06:11 PM #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?
If you don't understand my response, don't ignore it, ask a question.
 05142016, 06:39 PM #7Senior 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.
((13)^2 + (24)^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,
JimLast edited by jim829; 05142016 at 06:50 PM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 05142016, 07:17 PM #8Member
 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 ??
 05142016, 07:39 PM #9
Re: Different results from euclidean distance program
t how the euclidean distance calculate for 2D array
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

How to you write a java program using the distance formula?
By fscj in forum New To JavaReplies: 6Last Post: 09272013, 09:33 AM 
How to you write a java program using the distance formula?
By fscj in forum Math ExamplesReplies: 6Last Post: 09272013, 09:33 AM 
diving distance program
By maximumammo in forum New To JavaReplies: 4Last Post: 04052012, 08:09 PM 
Checking results after running program, without printing
By Tien in forum New To JavaReplies: 1Last Post: 11172011, 06:59 PM
Bookmarks