1
votes

I've implemented a custom distance function for k-medoids algorithm in Matlab, following the directions found in pdist.

Basically it compares two vectors, say A and B (which can also have different lengths) and checks if their elements "co-occur with tolerance": A(i) and B(j) co-occur with tolerance tol if

abs( A(i) - B(j) ) <= tol

Without going into details, the distance is large if there are few "co-occurrences with tolerance".

Everything works as I expect if I define tol as a constant inside the function, but now I would like to pass it as a parameter whenever I call k-medoids. pdist documentation does not mention this possibility:

A distance function specified using @: D = pdist(X,@distfun). A distance function must be of form d2 = distfun(XI,XJ), taking as arguments a 1-by-n vector XI, corresponding to a single row of X, and an m2-by-n matrix XJ, corresponding to multiple rows of X. distfun must accept a matrix XJ with an arbitrary number of rows. distfun must return an m2-by-1 vector of distances d2, whose kth element is the distance between XI and XJ(k,:).

So, is it possible to pass parameters in some way to a custom distance function in Matlab? If not, which alternatives should I consider?

1
Why not just abs(bsxfun(@minus, A(:), B(:).')) <= tol? What's the exact result you want?Luis Mendo
I call k-medoids as [idx, C] = kmedoids(data,2,'Distance',@custom_distance);. I would like to pass to custom_distance a value for tol. (the distance itself works, I used ismembertol). For now, I'm just specifying tol as a constant inside custom_distance.DanieleT

1 Answers

3
votes

To answer your general question, yes you can pass custom parameters to your custom distance function. You can define distfun in this way

a = 1; % Variable you want to pass to your function
distanceFunction = @(xi, xj)yourCustomDistanceFunction(xi, xj, a)

yourCustomDistanceFunction should accept the default parameters as the first two inputs and then the last input is your own variable (that isn't passed by pdist).

Then provide it to pdist in the following way

pdist(X, distanceFunction)