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?
abs(bsxfun(@minus, A(:), B(:).')) <= tol
? What's the exact result you want? – Luis Mendo[idx, C] = kmedoids(data,2,'Distance',@custom_distance);
. I would like to pass tocustom_distance
a value fortol
. (the distance itself works, I usedismembertol
). For now, I'm just specifyingtol
as a constant insidecustom_distance
. – DanieleT