matlabdistancepdist

Different behaviour for pdist and pdist2


In a MATLAB code I am using the kullback_leibler_divergence dissimilarity function that can be found here.

I have a matrix A and I compute the dissimilarity matrix using the downloaded function.

In theory, if I calculate

clear
A = rand(132,6); % input matrix
diss_mat = pdist(A,'@kullback_leibler_divergence'); % calculate the dissimilarity
square_diss_mat = squareform(diss_mat); % I put the dissimilarities in a square matrix
one_dist = pdist2(A(1,:),A,@kullback_leibler_divergence);

I should get the first row of square_diss_mat equal to one_dist, but I am not.

If I use the Euclidean distance I get it:

diss_mat = pdist(A);
square_diss_mat = squareform(diss_mat);
one_dist = pdist2(A(1,:),A); 

Could you please tell me why?


Solution

  • The kullback_leibler_divergence is not symmetric, thus the order matters:

    one_dist = pdist2(A, A(1,:), @kullback_leibler_divergence);
    

    I don't see any practical application using a non-symmetric function with pdist or pdist2.