how can i find the eucledian disatnce using KNN algorithm for the set of large n*2 matrix for the following kind of dataset

1 Ansicht (letzte 30 Tage)
1.739631e-01 6.457726e-01
1.716590e-01 6.457726e-01
1.693548e-01 6.457726e-01
1.647465e-01 6.457726e-01
so on..
These are stored in a .txt file, but how do I find the Euclidean distance for the current sample xy coordinate with other stored .txt 5 files to recognize the similar sample or min distance? I am not getting how to find this. Please can some one help me?

Antworten (1)

Kushagr Gupta
Kushagr Gupta am 8 Nov. 2016
I understand that you want to find euclidean distance between xy coordinates contained in different files.
Following is one way in which it can be done:
  1. Use the Import Tool in MATLAB to import the data into MATLAB workspace, say as a Matrices. Thus importing first file would result in a variable "data1" and second file would result in "data2".
  2. Assuming the files are of nx2 dimension (containing x coordinate as 1st column and y coordinate as 2nd column), the variables will also be nx2.
  3. Find the difference vector between the two variables.
  4. Use (dot-exponent) operator to raise it to required power.
  5. Use the 'sum' function along 2nd dimension and then 'sqrt' function to get the euclidean distance.
In code it can be done in a single line for two files and can be applied to multiple files using a loop:
>> dist = sqrt(sum((data1-data2).^2),2))
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by