How to determine shortest path to the CH?
1 次查看(过去 30 天)
显示 更早的评论
Hi, i need some help, how to build the edge in random nodes and determine the shortest path to CH as following figure.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195252/image.png)
Code.
clc;
clear all;
N=10;
fontSize = 12;
data = rand(N,2); % Randomly generated n no. of nodes
x = data(:,2)*100;
y = data(:,1)*100;
plot(x, y, 'bo', 'LineWidth', 1, 'MarkerSize', 8);
grid on
axis square;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
for k = 1 : N
text(x(k), y(k), num2str(k), 'FontSize', 12, 'Color', 'b');
end
node=struct('id',{},'x',{},'y',{},'dist',{},'link',{'1'});
for i=1:N
node(i).id=i;
node(i).x=x(i);
node(i).y=y(i);
text(x(i),y(i),num2str(i));
end
ch1=ceil(rand(1)*N);
text(node(ch1).x,node(ch1).y,['CH1'],'Color' ,'r');
for i = 1:N
node(i).dist = sqrt(((node(ch1).x - node(i).x)^2 + (node(ch1).y - node(i).y)^2));
a(i)=node(i).dist;
disp(node(i).dist);
end
A=sort(a,'descend');
a=1;
for i = 1:N
node_distance(a) = node(i).dist;
a = a+1;
end
Maximum_distance = max(node_distance);
for i = 1:N
if node(i).dist == max(node_distance)
next_header = node(i).id;
end
end
Please advice,
Thanks
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!