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.
Best Answer