programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Euclidean distance calculation

nvayin agha
Greenhorn
Posts: 16
Hi,

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

the code is :

Knute Snortum
Sheriff
Posts: 4281
127
Sorry, are you asking why the results are different?

nvayin agha
Greenhorn
Posts: 16
Yes , I do not understand why they are not same ?

Knute Snortum
Sheriff
Posts: 4281
127
You're summing the square of the difference in calculateDistance but not in Euclideanorm.

nvayin agha
Greenhorn
Posts: 16
Yes , i realize that , how we can calculate euclidean distance in this case : data is 2D ??

Paul Clapham
Sheriff
Posts: 22832
43
In "this" case meaning which of the two cases you have there?

You are already calculating distance correctly in your calculateDistance method; the other method I don't know whether it's correct because I don't know what it's supposed to do. It has no comments explaining its purpose and a name which doesn't mean anything to me.

nvayin agha
Greenhorn
Posts: 16
@paul the second method euclideanorm is same euclidean distance but the data which i use with second method is 2D array , iam confused with it ?

Piet Souris
Master Rancher
Posts: 2044
75
hi nvayin,

what you do in your method 'Euclideanorm' is to calculate the sum of all the elements, and returning the square root of that sum.
Now, I don't know what the mathematical meaning of this is.
But given a 2D array, you can calculate the distance between any two columns or any two rows, like you do in your 'calculateDistance' method.

Knute Snortum
Sheriff
Posts: 4281
127
nvayin agha wrote:Yes , i realize that , how we can calculate euclidean distance in this case : data is 2D ??

Well, you don't really need the second for-loop. Use index [0] for one element and index [1] for the other.

Paul Clapham
Sheriff
Posts: 22832
43
Knute Snortum wrote:Well, you don't really need the second for-loop. Use index [0] for one element and index [1] for the other.

I suppose that might be the meaning of that mysterious "Euclideanorm" thing. But we haven't been told that yet, even though several people have asked.