find the distance from 1 point to all point

조회 수: 5 (최근 30일)
ha ha
ha ha 2017년 10월 26일
댓글: ha ha 2017년 10월 26일
Let's say: I have the data A containing of n points
A=[ 5 5 1 ----> coordinate (x,y,z) of point 1
4 5 2 ----> coordinate (x,y,z) of point 2
1 1 1 ----> coordinate (x,y,z) of point 3
2 3 1 ----> coordinate (x,y,z) of point 4
6 1 7 ----> coordinate (x,y,z) of point 5
................................................
....... ] ----> coordinate (x,y,z) of point n
And, I also have point: B(0 2 1).
How to find the Euclidean distance between point B and all points(1,2,3,4,5,...,n) in A.
Example:
distance between point B and points 1:
=sqrt{(5-0)^2+ (5-2)^2 + (1-1)^2}= 5.83

채택된 답변

Rik
Rik 2017년 10월 26일
The implicit expansion introduced in R2016b can make life easier (here it can make the command a bit shorter), but I'm going to assume you don't have it.
distance=sqrt((A(:,1)-B(1)).^2+(A(:,2)-B(2)).^2+(A(:,3)-B(3)).^2);
  댓글 수: 3
Jan
Jan 2017년 10월 26일
편집: Jan 2017년 10월 26일
@ha ha: Use sort to sort them and then take the first 10 values. If you need the indices also, get the 2nd output of sort.
With auto-expanding since R2016b:
Dist = sqrt(sum((A - B) .^ 2, 2))
ha ha
ha ha 2017년 10월 26일
@ Jan Simon. THANK

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!