How is the correct way to calculate distance of each data points between two vectors

조회 수: 1 (최근 30일)
Hi, I would like to compute distance between two vectors. However, the result does not yields the expected outcome. First code:
X = rand(3,2)
D0 = pdist(X)
D1 = pdist(X,'minkowski')
X =
0.8147 0.9134
0.9058 0.6324
0.1270 0.0975
D1 =
0.2954 1.0670 0.9448
If i generate the random 3x2 data, the output yields as expectation. However, if i'm using the same function on the vectors, the result appears in the weird way as below Second code::
X1 = [4,10,1,1,2,7,12,5,6,10];
X2 = [3,9,7,8,2,7,12,5,1,3];
X = [X1;X2]'
output:
X =
4 3
10 9
1 7
1 8
2 2
7 7
12 12
5 5
6 1
10 3
D1 =
Columns 1 through 10
8.4853 5.0000 5.8310 2.2361 5.0000 12.0416 2.2361 2.8284 6.0000 9.2195
Columns 11 through 20
9.0554 10.6301 3.6056 3.6056 6.4031 8.9443 6.0000 1.0000 5.0990 6.0000
Columns 21 through 30
12.0830 4.4721 7.8102 9.8489 6.0828 6.0828 11.7047 5.0000 8.6023 10.2956
Columns 31 through 40
7.0711 14.1421 4.2426 4.1231 8.0623 7.0711 2.8284 6.0828 5.0000 9.8995
Columns 41 through 45
12.5300 9.2195 4.1231 5.3852 4.4721
I do wonder, why do the results must be 45 points of distance. By right, the result is supposed to produce 11 points of distance because my purpose it to compute the distance of each data points in vector 1 and vector 2 and i also just have 11 data points in vector 1 and vector 2. Appreciate if anyone could advice me to improve my code. Thanks

채택된 답변

javad ebrahimi
javad ebrahimi 2018년 2월 19일
Hi,your Pairwise distance type calculate:
this code can help you
clc
clear
X = rand(3,2);
D1 = length(X(:,1)-X(:,2));
  댓글 수: 1
amj
amj 2018년 2월 19일
Dear Javad ebrahimi,
THanks for your advice. How about if i want to calculate the distance on these 2 vectors?
X1 = [4,10,1,1,2,7,12,5,6,10];
X2 = [3,9,7,8,2,7,12,5,1,3];
X = [X1;X2]'

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

javad ebrahimi
javad ebrahimi 2018년 2월 24일
Hi amj you can use this code:
clc
clear
X1 = [4,10,1,1,2,7,12,5,6,10];
X2 = [3,9,7,8,2,7,12,5,1,3];
X = [X1;X2]'
D1 = length(X(:,1)-X(:,2));

카테고리

Help CenterFile Exchange에서 Computational Geometry에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by