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
0 个评论
采纳的回答
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 个)
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 Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!