0

Welcome!

I have a set of n matlab matrix with the following structure:

xyz_1   -3,37200000000000   2,80000000000000    5,03400000000000

xyz_2   -2,21700000000000   1,74500000000000    7,45300000000000

....    ..................  ................    ................

xyz_n  -1,39300000000000    0,00700000000000000 6,35500000000000

where the first column is the name of matrix, while the next three columns are the xyz coordinates. I' am looking for an efficient way to find the nearest neighbor. I would like to give the matrix name and k of potential neighbors as the input parameter then program will find the nearest neighbors giving me the result matrix in the following form:

[nearest_neighbor_name_1;    distance_between_quoted_element_and_nearest_neigbor_1

 nearest_neighbor_name_2;    distance_between_quoted_element_and_nearest_neigbor_2

 nearest_neighbor_name_....; distance_between_quoted_element_and_nearest_neigbor_....

 nearest_neighbor_name_k;    distance_between_quoted_element_and_nearest_neigbor_k]

I tried to use the knnsearchunfortunately without effect. Thank you for your help!

destrudos
  • 27
  • 6
  • Please reformat the question using \` for code segments. Also: What do you mean by: *I tried to use `knnsearch` without effect*? Why was this approach unsatisfactory? – knedlsepp Dec 27 '14 at 18:12
  • I found the description how to searching two dimensional coordinates. Unfortunately I have no idea how to use the `knnseach` in the case of a three-dimensional system. In addition, the input parameter for `knneseach` are single matrices (X and Y) not, as in my case, a set of multi-array. – destrudos Dec 27 '14 at 18:48
  • Look at [this post](http://stackoverflow.com/questions/27475978/finding-k-nearest-neighbour-with-matlab/27476929#27476929) for insight. Make sure you format your data such that each row is a point and each column is a variable. – rayryeng Dec 28 '14 at 02:25

1 Answers1

0

Is the traditional way of unsatisfactory in some way? Evaluate the distance from each point to your test point and then sort the distances...

%define the "k" entries that you are interested in assessing
mydata_xyz = [-3.37200000000000   2.80000000000000    5.03400000000000;
              -2.21700000000000   1.74500000000000    7.45300000000000;
                        <the rest of your data here>
              -1.39300000000000    0.00700000000000000 6.35500000000000];

%define the point about which you are looking for the nearest neighbors
mypoint_xyz = [ <whatever your xyz coordinate is];


%compute the distance from the given point to all of the other test points.
%Note the use of the transpose (apostrophe) to ensure that it sums in the
% correct direction
distance = sqrt(sum( ((mydata_xyz - ones(size(mydata_xyz,1),1)*mypoint_xyz).^2)' )); 

%sort to get the nearest neighbor followed by the next nearest neighbors
[sorted_distance, Isort] = sort(distance);

%print out each one of the points, from closest to farthest
for I=1:length(Isort)
    disp(['Point ' num2str(Isort) ...
          ', dist = ' num2str(distance(Isort(I))) ...
          ', xyz = ' num2str(mydata_xyz(Isort(I),:))]);
end
chipaudette
  • 1,655
  • 10
  • 13