How to plot lines between points

9 Ansichten (letzte 30 Tage)
KonVak
KonVak am 3 Okt. 2022
Kommentiert: William Rose am 3 Okt. 2022
I Have a matrix of routes (11x11)
1 5 46 1 0 0 0 0 0 0 0
1 41 10 40 73 13 27 1 0 0 0
1 31 75 49 30 16 58 14 28 53 1
1 68 36 20 55 9 47 35 1 0 0
1 7 34 24 57 64 1 0 0 0 0
1 4 45 25 50 17 52 1 0 0 0
1 59 11 32 26 56 19 51 33 18 1
1 2 44 42 43 65 23 62 22 29 1
1 48 37 70 72 61 71 21 38 6 1
1 76 69 3 63 74 1 0 0 0 0
1 39 12 66 67 60 15 54 8 1 0
This means that the vehicle will go from 1 to 5 to 46...and return to 1 again. There are 11 different routes.
I have plotted already
for i=1:N
pltx(i)=z(i,2)
plty(i)=z(i,3)
pltz(i)=z(i,1)
end
n=numel(pltx);
plot( graph(1:n,1:n),'LineStyle','none','Marker','d','XData',pltx,'YData',plty);
z(Nx3) is a matrix with the coordinates of each point.
Now i would like to plot a line that would follow the order in the matrix routes connecting the points with z coordinates.
The outcome im looking for is this:
or something close. Any ideas on what else i would need to add to my code to make it work ?

Akzeptierte Antwort

William Rose
William Rose am 3 Okt. 2022
N=76; %number of points
z=80*rand(N,3); %point coordinates
routes=[1 5 46 1 0 0 0 0 0 0 0;
1 41 10 40 73 13 27 1 0 0 0;
1 31 75 49 30 16 58 14 28 53 1;
1 68 36 20 55 9 47 35 1 0 0;
1 7 34 24 57 64 1 0 0 0 0;
1 4 45 25 50 17 52 1 0 0 0;
1 59 11 32 26 56 19 51 33 18 1;
1 2 44 42 43 65 23 62 22 29 1;
1 48 37 70 72 61 71 21 38 6 1;
1 76 69 3 63 74 1 0 0 0 0;
1 39 12 66 67 60 15 54 8 1 0];
routes(routes==0)=1; %changes 0s to 1s to make plotting simpler
scatter(z(:,2),z(:,3),'d',"filled"); %plot points
a = num2str([1:N]'); labels=cellstr(a);
text(z(:,2)+1,z(:,3)+1,labels,'fontsize',8); %label each point
hold on;
for i=1:11
plot(z(routes(i,:),2),z(routes(i,:),3))
end
Try it. Good luck.
  2 Kommentare
KonVak
KonVak am 3 Okt. 2022
Thank you, that worked more than fine.
William Rose
William Rose am 3 Okt. 2022
@KonVak, you're welcome.

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

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by