Search code examples
matlabdistancepdist

Minkowski distance and pdist


Minkowski's distance equation can be found here.

If we want to calculate the Minkowski distance in MATLAB, I think we can do the following (correct me if I'm wrong):

dist=pdist([x(i);y(j)],'minkowski');

Up till here, the above command will do the equation shown in the link.

Now, to Minkowski's distance, I want to add this part |-m(i)|^p, where m(i) is some value.

I saw the pdist source code, but, want to ask, how can I modify Minkowski's distance by adding this simple part, either in the pdist code, or from the calling code (I think we may have to make some change in the parameters).

Thanks.


Solution

  • I think this does what you want: define a custom distance function and use it as an argument to pdist:

    p = 2;
    fun = @(x,y) sum(abs(x-y).^p + abs(m(i)).^p ).^(1/p);
    pdist([x(i);y(j)],fun)
    

    Or directly use

    sum(abs(x(i)-y(j)).^p + abs(m(i)).^p ).^(1/p)
    

    I'm assuming i and j are just indices that run through the elements of x and y.