I have implemented this KNN inner distance equation for the output of the knnsearch
function.
Example
Main dataset is `{ 1 ;2; 5; 10; 20 }`
Query dataset is `{ 3 }`
calculate the distance for all point
k=3
Discard 3-3=0
3-1=2
3-2=1
3-5=2
3-10=7
3-20=17
so {1;2;5} will be nearest neighbours for 3
to calculate knn in the equation
Matrix point
p 1 2 5
1 0 1 4
2 1 0 3
5 4 3 0
calculate knn-inner distance= (1+4+1+3+4+3)/6
My code is part of LDOF outlier algorithm when I checked my code, I found this part not accurate for a multidimensional array. This is the first time using pdist function, I have used it for speed.
[Idx,Dist] = knnsearch(main,query,'k',10);
sizedata=size(Idx)
Dxp=mean(pdist(main(Idx(1,2:end),:)));
for m =2 : sizedata
d=Dxp;
Dxp=mean(pdist(main(Idx(m,2:end),:)));
% to make matrix with all knn-inner distance for all query points
Dxp=[d;Dxp];
end
I have Matlab 2018.