how to find euclidean distance between one vector and many other vectors

6 ビュー (過去 30 日間)
fathi
fathi 2017 年 4 月 22 日
編集済み: Image Analyst 2019 年 10 月 2 日
Hi
I like to calculate Euclidean distance between my first matrix, which contains from one vector, and many other matrices which have similar dimensions ( row vectors ) and determine the smallest distance, and which matrix has it.
Example:
if I have D = [ 1 2 4]
and I1 = [3 5 5 ] , I2 = [ 5 7 8 ], I3 = [ 9 8 7 ] , I4 = [ 1 2 3 ]
so after calculating the Euclidean distance between D AND I1,I2,I3 and I4 the smallest distance will be between D and I4. D as reference
How can I do that????
Thank you

採用された回答

Image Analyst
Image Analyst 2017 年 4 月 22 日
編集済み: Image Analyst 2019 年 10 月 2 日
Try pdist2() if you have the Statistics and Machine Learning Toolbox
D = [ 1 2 4]
I1 = [3 5 5 ]
I2 = [5 7 8 ]
I3 = [9 8 7 ]
I4 = [1 2 3 ]
IAll = [I1;I2;I3;I4]
distances = pdist2(D, IAll)
[minDistance, indexOfMinDistance] = min(distances)
You'll see:
D =
1 2 4
I1 =
3 5 5
I2 =
5 7 8
I3 =
9 8 7
I4 =
1 2 3
IAll =
3 5 5
5 7 8
9 8 7
1 2 3
distances =
3.74165738677394 7.54983443527075 10.4403065089106 1
minDistance =
1
indexOfMinDistance =
4
indexOfMinDistance will be 4, and since I put I4 into row 4, I4 has the closest distance to D.
  2 件のコメント
fathi
fathi 2017 年 4 月 24 日
Thanks a lot for this answer, clear.
NISHANT GUPTA
NISHANT GUPTA 2019 年 10 月 1 日
編集済み: NISHANT GUPTA 2019 年 10 月 1 日
Hello Image Analyst you are a saviour i was initially thinking that i need to code this
I have data set of about 9000 points in 103 cell arrays and need to find this for each point in one array to another array and map the index for each point
Thanks

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2017 年 4 月 22 日
doc pdist

カテゴリ

Help Center および File ExchangeStatistics 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