Effacer les filtres
Effacer les filtres

Draw lines from one point to the all others

2 vues (au cours des 30 derniers jours)
Asim Ismail
Asim Ismail le 7 Mai 2017
Commenté : Star Strider le 7 Mai 2017
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.

Réponse acceptée

Star Strider
Star Strider le 7 Mai 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 commentaires
Asim Ismail
Asim Ismail le 7 Mai 2017
Thank you very much Star Strider, though you have done it in a different way but the result is same
Star Strider
Star Strider le 7 Mai 2017
My pleasure.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by