0

I would like to use the linkage function in matlab with a custom distance.

My distance function is in the form:

Distance = pdist(matrix,@mydistance); 

so given a

matrix = rand(132,18)

Distance will be a vector [1x8646];

D_matrix = squareform(Distance,'tomatrix');

is a matrix 132x132 contaning all the pairwise distances between te rows of matrix

How can I embed mydistance in linkage?

Cape Code
  • 3,584
  • 3
  • 24
  • 45
gabboshow
  • 5,359
  • 12
  • 48
  • 98

2 Answers2

0

You can use a call to linkage like this:

Z = linkage(Data,'single','@mydistance')

where 'single' can also be any of the other cluster merge methods as described here: http://www.mathworks.com/help/stats/linkage.html.

In other words, just put your function handle in a string and pass it as the 3rd argument to linkage. You cannot use the 'savememory' function in linkage while using a custom distance function, however. This is causing me some frustration with my 300,000 x 6 dataset. I think the solution will be to project it to some space where euclidean distance is defined and meaningful but we'll see how that goes.

0

Besides using

tree = linkage(Data,'single','@mydistance')

like Imperssonator suggests, you can also use

dissimilarity = pdist(Data,@mydistance);
tree = linkage(dissimilarity,'single');

The latter has the benefit of allowing Data to be an object array with @mydistance using objects as arguments.

Memto
  • 15
  • 5