# Combination of distances between multiple points

조회 수: 6(최근 30일)
Mirza Ahmed 2021년 7월 29일
댓글: Mirza Ahmed 2021년 8월 3일
I have a number of circles, where I can define the number at the beginning, i = number of circle. I have to calculate every minimum distance between every circle. That means the equation can be written as:
dist = (x1-x2)^2 + (y1-y2)^2 - (r1+r2)
Let's say the number of circle is chosen 6, so the number of combination of distances come as 15.
To define the variables first, I have declared the matrices of radius, x coordiantes and y coordinates. Now, from this point onwards, I need help to write the basic and generalized code to find the combination.
Thanks!

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

### 채택된 답변

Rik 2021년 7월 29일
You already have the generalized code. You just need to combine it with nchoosek:
x=1+10*rand(1,6);
y=1+10*rand(1,6);
r=rand(1,6);
combs=nchoosek(1:6,2);
c1=combs(:,1);c2=combs(:,2);
d=(x(c1)-x(c2)).^2 + (y(c1)-y(c2)).^2 - (r(c1)+r(c2));
combs(:,3)=d
combs = 15×3
1.0000 2.0000 7.9112 1.0000 3.0000 8.4890 1.0000 4.0000 8.4228 1.0000 5.0000 5.9644 1.0000 6.0000 21.6430 2.0000 3.0000 28.6815 2.0000 4.0000 34.0819 2.0000 5.0000 1.8794 2.0000 6.0000 11.5847 3.0000 4.0000 3.2875
%(circle 1, circle 2, distance)
##### 댓글 수: 1표시숨기기 없음
Mirza Ahmed 2021년 8월 3일
Thanks, man

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

### Community Treasure Hunt

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

Start Hunting!