find the distance from each point in matrix B to all point in matrix A

2 ビュー (過去 30 日間)
ha ha
ha ha 2017 年 10 月 26 日
コメント済み: Rik 2017 年 10 月 27 日
Let's say: I have the matrix A containing of 5 points
A=[ 5 5 1 ----> coordinate (x,y,z) of point A1
4 5 2 ----> coordinate (x,y,z) of point A2
1 1 1 ----> coordinate (x,y,z) of point A3
2 3 1 ----> coordinate (x,y,z) of point A4
6 1 7 ] ----> coordinate (x,y,z) of point A5
And, I also have matrix B:
B=[ 0 2 1 ----> coordinate (x,y,z) of point B1
4 1 1 ] ----> coordinate (x,y,z) of point B2
How to find the Euclidean distance between each point in B and all points(1,2,3,4,5) in A.
Example:
distance between point B1 and points A1:
=sqrt{(5-0)^2+ (5-2)^2 + (1-1)^2}= 5.83
distance between point B1 and points A2
distance between point B1 and points A3
.......................................
distance between point B2 and points A1
distance between point B2 and points A2, A3,A4, A5
*I hope the result will be shown in matrix C(2-by-5)*
  1 件のコメント
Rik
Rik 2017 年 10 月 26 日
Knowing my answer on your previous question, what do you think would be the solution? It isn't that hard. Of course you could solve this with a loop, but there is a direct solution, for which you can use functions like repmat.
Show some effort.

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 10 月 26 日
編集済み: Andrei Bobrov 2017 年 10 月 26 日
Or
Dista = squeeze(sqrt(sum((A - reshape(B',1,3,[])).^2,2))); % New MATLAB
Dista = squeeze(sqrt(sum(bsxfun(@minus,A,reshape(B',1,3,[])).^2,2))); % Old MATLAB

その他の回答 (1 件)

KSSV
KSSV 2017 年 10 月 26 日
doc pdist and pdist2.
  4 件のコメント
Andrei Bobrov
Andrei Bobrov 2017 年 10 月 27 日
Rik
Rik 2017 年 10 月 27 日
Typing doc pdist will open the documentation for the pdist function, which you can also find online. If you look at the 'see also' section, you will a find reference to the pdist2 function.
Knowing how to look for and read the documentation is a very useful skill. Extremely easy to learn, sometimes hard to master. Using your favorite internet search engine also helps out a lot.

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

カテゴリ

Help Center および File Exchange行列および配列 についてさらに検索

Community Treasure Hunt

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

Start Hunting!