Draw lines from one point to the all others
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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 —
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!