Unexpected pdist2 behavior?

According to the documentation, the function
D = pdist2(X,Y,distance)
returns the pairwise distance between rows in X and rows in Y, where the distance, specified with
distance
can be a custom function handle. So for example, I know that all the points in X and Y all lie on the same 3D cylinder, and I want the distance to be the cylindrical geodesic distance, I could set
distance = @cylindricalGeodesic;
However, what I get when using this is not the pair-wise distance between points in X and points in Y:
>> a = [ 1 1 0; -1 1 0];
>> b = [sqrt(2) 0 1;1 1 1];
>> pdist2(a,b,@cylindricalGeodesic)
ans =
1.1107 2.2214
1.1107 2.2214
>> for i = 1:2;for j = 1:2;d(i,j) = cylindricalGeodesic(a(i,:),b(j,:));end;end;d
d =
1.4946 1.0000
3.4790 2.4361
I fully accept this may just be me overlooking something silly, but this behavior seems very unexpected to me: that pdist2 is not equivalent to the pairwise application of the distance function?

4 个评论

I can't seem to find a TMW-supplied function cylindricalGeodesic; what's it?
A custom-defined function
function d = cylindricalGeodesic(x,y)
that computes the distance of the cylindrical geodesic connecting x and y, where x,y are 1 by 3 (it verifies the points are co-cylindrical internally)
Actually, I think I just answered my own question: the documentation makes it seem like the second argument of the distance function must be able to handle an arbitrary number of rows? Is that true?
I just modified the function so that y is an arbitrary number of rows, and it works now:
>> pdist2(a,b,@cylindricalGeodesic)
ans =
1.4946 1.0000
3.4790 2.4361

回答(1 个)

Bruno Luong
Bruno Luong 2018-11-5

0 个投票

In the DOC of PDIST2 it has been mentioned that the custom distance must able to accept second parameter of (m x 3) as m points in R^3.
I suspect you don't do it and this screws up PDIST2

此问题已关闭。

标签

提问:

2018-11-5

关闭:

2021-8-20

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by