Defining effective distance function in matlab

I am using the kNN search function in matlab, but I am calculating the distance between two objects of my own specific class, so I wrote a new distance function. It's him:

         function d = allRepDistance(obj1, obj2)
         %calculates the min dist. between repr.
         % obj2 is a vector, to fit kNN function requirements

            n = size(obj2,1);
            d = zeros(n,1);
            for i=1:n
                    M =  dist(obj1.Repr, [obj2(i,:).Repr]');
                    d(i) = min(min(M));
            end

     end

      

The difference is what obj.Repr

can be a matrix and I want to calculate the minimum distance between all rows of each argument. But even if obj1.Repr

it is just a vector that gives the essentially normal Euclidean distance between two vectors, the kNN function is 200 times slower!

I tested the performance of distance function only (no kNN). I measured the time it takes to calculate the distance between the vector and the rows of the matrix (when they are in the object) and it runs 3 times slower and then the normal distance function.

Is that the point? Is there a solution?

0


source to share


1 answer


You are using dist()

that corresponds to the Euclidean distance weighting function. However, you are not weighing your data i.e. You don't think one dimension is more important to others. So you can use Euclidean distance directly pdist()

:

 function d = allRepDistance(obj1, obj2)
 % calculates the min dist. between repr.
 % obj2 is a vector, to fit kNN function requirements
    n = size(obj2,1);
    d = zeros(n,1);
    for i=1:n
        X = [obj1.Repr, obj2(i,:).Repr'];
        M = pdist(X,'euclidean');
        d(i) = min(min(M));
    end
end

      



By the way, I don't know your matrix sizes, so you will need to deal with element concatenation to create it correctly X

.

0


source







All Articles