Source

ITE / code / H_I_D_A_C / utilities / kNN_squared_distances.m

Diff from to

File code/H_I_D_A_C/utilities/kNN_squared_distances.m

 
 switch co.kNNmethod
     case 'knnFP1'%fast pairwise distance computation and C++ partial sort
-        if Y_equals_to_Q %'Y==Q'
+        if Y_equals_to_Q %'Y==Q' => exclude the points themselves
             [squared_distances,indices] = knn(Q, Y, max(co.k)+1);%assumption below:max(co.k)+1 <= size(Q,1)[=size(Y,1)]
             squared_distances = squared_distances(2:end,:);
             indices = int32(indices(2:end,:));
             indices = int32(I(1:max(co.k),:));
         end
     case 'knnsearch' %Statistics Toolbox:Matlab
-        [indices,distances] = knnsearch(Y.',Q.','K',max(co.k),'NSMethod',co.NSmethod); %[double,...
-        indices = int32(indices.');%.': to be compatible with 'ANN'
-        squared_distances = (distances.').^2;%distances -> squared distances; .': to be compatible with 'ANN'
+        if Y_equals_to_Q %'Y==Q' => exclude the points themselves
+            [indices,distances] = knnsearch(Y.',Q.','K',max(co.k)+1,'NSMethod',co.NSmethod); %[double,...
+            indices = int32(indices(:,2:end).');%.': to be compatible with 'ANN'
+            squared_distances = (distances(:,2:end).').^2;%distances -> squared distances; .': to be compatible with 'ANN'
+        else
+            [indices,distances] = knnsearch(Y.',Q.','K',max(co.k),'NSMethod',co.NSmethod); %[double,...
+            indices = int32(indices.');%.': to be compatible with 'ANN'
+            squared_distances = (distances.').^2;%distances -> squared distances; .': to be compatible with 'ANN'
+        end
     case 'ANN'%ANN library/wrapper
         if working_environment_Matlab
             ann_object = ann(Y);