I found the sample code to calculate distance of two matrix using euclidean distance from here: Finding K-nearest neighbors and its implementation The data matrix are as below:
load fisheriris
X = meas(:,3:4);
newpoints = [5 1.45; 7 2; 4 2.5; 2 3.5];
How i'm going to apply the Chebyshev and Mahalanobis distance and replace the function below:
%// Use Euclidean
dists = sqrt(sum(bsxfun(@minus, x, newpoint).^2, 2));
I tried to change the code as :
dists = max(abs(bsxfun(@minus, X, newpoint)))
The answer is as below. May be because i put the max function based on the formula.
dists2 =
4.0000 1.3500
But, if i used this knnsearch code, it is work as expected. But i need to apply the bsxfun so that my code will be standardized with the upper codes. I want to compare the different distances in my algorithm:
[ncb,dcb] = knnsearch(X,newpoint,'k',10,'distance','chebychev')
Appreciate if anyone could help me.
you can find the extented version of my answer here:
the quintessence:
load fisheriris
oldpoints = meas(:,3:4);
newpoints = [5 1.45; 7 2; 4 2.5; 2 3.5];
newpoints = permute(newpoints, [3,2,1]);
% Euclidean distance
dists_euclid = sqrt(sum(bsxfun(@minus, newpoints, oldpoints).^2, 2));
% Chebyshev distance
dists_cheby = max(abs(bsxfun(@minus, oldpoints, newpoints)),[],2);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.