how to find distance matrix using knnclassify in matlab?

I work in image classication and I used two classifiers : svm et knn. the problem is that the knnclassify matlab function allows only return a list of labels test images (class) and I need to get out the distance matrix
class = knnclassify( TestData , TrainData , TrainLabels);
please help me how can I find the distance matrix ?? and thanks in advance

 Accepted Answer

You likely have to use knnsearch to get the distances. See the documentation on Classification Using Nearest Neighbors for details.

4 Comments

thank you for your answer, in this example I used a train matrix xapp size 10 * 4 and one for test xtest size 5 * 4 , when I use knnsearch I only get a vector that contains the closest distance but my goal is to have all the distances between samples. is there a way to do it? thanks in advance
load(xapp.mat)
load(xtest.mat)
[n,d]=knnsearch(xapp,xtest,'k',1,'distance','cityblock');
n =
1
4
5
6
10
d =
0.0441
0.0762
0.0100
0.0354
0.0189
If I understand correctly, the pdist or pdist2 functions may be what you want.
thank you, that's exactly the answer
My pleasure! I’m glad it worked.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!