Filter löschen
Filter löschen

Draw lines from one point to the all others

2 Ansichten (letzte 30 Tage)
Asim Ismail
Asim Ismail am 7 Mai 2017
Kommentiert: Star Strider am 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.

Akzeptierte Antwort

Star Strider
Star Strider am 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 Kommentare
Asim Ismail
Asim Ismail am 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 am 7 Mai 2017
My pleasure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by