How does pdist2 work?

76 次查看(过去 30 天)
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?

采纳的回答

John D'Errico
John D'Errico 2016-9-3
编辑:John D'Errico 2016-9-3
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.
  2 个评论
Luciano La Frazia
Luciano La Frazia 2017-11-9
Hi I feel kind of silly making this question after your excellent answer, but I can't figure out how pdist2 works. I want to get the euclidean distance between two matrixes.
A = 131.1903 36.2473
54.8736 281.1548
102.8072 281.0851
125.1274 269.3642
155.6719 144.6703
26.7995 191.9928
B = 123.7503 128.2180
127.8487 159.0561
136.1987 132.0602
75.6246 13.0668
48.9525 21.3157
153.4662 104.5798
Applying pdist2 I get this
D = 92.2711 167.7309 154.2950 141.1529 35.9120 116.0460
122.8542 142.2444 124.5719 110.3417 31.3222 106.2816
95.9437 169.8322 152.7201 137.7497 23.1996 124.7402
60.2071 268.8900 269.3933 261.0343 154.0359 185.4681
83.5824 259.9066 265.2932 259.4816 163.1116 172.1087
71.8717 202.2355 183.6313 167.2034 40.1511 153.9008
How pdist2 get the 154.2950 value for example?
Thanks in advance, sorry for answering the same with a larger example.
Mohammad Al ja'idi
the results that you should get by using the pdist2 are as follows :
92.2711 122.8543 95.9437 60.2070 83.5823 71.8717
167.7309 142.2444 169.8322 268.8899 259.9066 202.2356
154.2951 124.5719 152.7200 269.3932 265.2932 183.6313
141.1529 110.3417 137.7496 261.0343 259.4815 167.2034
35.9119 31.3222 23.1996 154.0359 163.1116 40.1511
116.0460 106.2815 124.7401 185.4681 172.1088 153.9009

请先登录,再进行评论。

更多回答(1 个)

Marcela Mantilla
Marcela Mantilla 2019-8-26
Buenas tardes¡¡ Como se interpretan los resultados de una matriz obtenida de la función pdist2. Muchas gracias.
  1 个评论
Walter Roberson
Walter Roberson 2019-8-26
编辑:Walter Roberson 2019-8-26
results(J, K) = distance between the point represented by row J of the first input and point represented by row K of the second input.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by