Draw lines from one point to the all others

3 views (last 30 days)
Can someone help me to draw lines between a central point to the all others. The points are created randomly, lets take 5 points.
xy = rand(5, 2);
distances = pdist2(xy, xy)
S = sum(distances)
This code will give all sum of distances between the points, now the point having minimum distance will be taken as central. I want to connect the other points with the central one. Something like this, i draw this in paint.

Accepted Answer

Star Strider
Star Strider on 7 May 2017
If I understand correctly what you want to do, this works.
The Code
xy = rand(5, 2);
centrd = mean(xy); % Calculate Centroid
[dist1,idx1] = pdist2(xy, centrd, 'euclidean', 'Smallest', size(xy,1)); % Distances From Centroid
centre_point = xy(idx1(1),:); % Point With Shortest Distance To Centroid
[distances,idx2] = pdist2(xy, centre_point, 'euclidean', 'Smallest', size(xy,1)); % Distances From Centre Point
Distance_Sum = sum(distances); % Sum Of Distances To Centre Point
figure(1)
plot(xy(:,1), xy(:,2), 'pg', 'MarkerSize',10, 'MarkerFaceColor','g')
hold on
plot([repmat(centre_point(1),size(xy,1),1) xy(:,1)]', [repmat(centre_point(2),size(xy,1),1) xy(:,2)]')
hold off
grid
text(centrd(1), centrd(2), sprintf('\\Sigma \\itdist\\rm = %.4f',Distance_Sum), 'FontSize',10)
The Plot
  2 Comments
Asim Ismail
Asim Ismail on 7 May 2017
Thank you very much Star Strider, though you have done it in a different way but the result is same

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!