MATLAB: How does pdist2 work

pdist

I am really confused. I have a matrix A=
1 2
3 4
I want to calculate dissimilarity. So I came to know that i could use euclidean to find.
pdist2(A,A)
gives me
0 2.828
2.828 0
But If I calculate manually I am getting
0 1.414
1.414 0
Why the result is multiplied by 2?

Best Answer

Because pdist2 computes the distances between ROWS of A. You computed the distance between the columns.
So the distance between the vector [1 2] and [3 4] is 2.8284... (or, 2*sqrt(2)).
A = [1 2;3 4];
norm(A(1,:) - A(2,:))
ans =
2.8284
Using pdist2:
pdist2(A,A)
ans =
0 2.8284
2.8284 0
See that if I transpose A, then compute distances, I get what you expected.
pdist2(A',A')
ans =
0 1.4142
1.4142 0
Read the help for pdist2:
pdist2 Pairwise distance between two sets of observations.
D = pdist2(X,Y) returns a matrix D containing the Euclidean distances
between each pair of observations in the MX-by-N data matrix X and
MY-by-N data matrix Y. Rows of X and Y correspond to observations,
That is, it works on the ROWS of the matrices.