如何为每个节点分配随机能量 E 并比较两个节点的最大能量并找到它们之间的距离?
在 gnovice 的帮助下,我得到了以下代码,但现在我想使用 E=floor(rand(1)*10)
为每个节点分配能量(随机)想要比较最大能量以及它们之间的距离是多少?
N=input('no. of nodes : '); %# Number of nodes
coords = num2cell(rand(N,2)) %# Cell array of random x and y coordinates
nodes=struct('x',coords(:,1),... %# Assign x coordinates
'y',coords(:,2)); %# Assign y coordinates
plot([nodes.x],[nodes.y],'r*'); %# Plot all the nodes in red
index = randi(N,[1 2]) %# Pick two nodes at random
hold on;
plot([nodes(index).x],[nodes(index).y],'b*'); %# Plot 2 random nodes in blue
index(1) %# which node is selected first.
index(2) %# which node is selected second.
这个问题是这个问题的后续问题。
As helped by gnovice I got following code, but now I want to assign energy (randomly) to every nodes using E=floor(rand(1)*10)
and also want to compare for maximum energy and what is distance between them?
N=input('no. of nodes : '); %# Number of nodes
coords = num2cell(rand(N,2)) %# Cell array of random x and y coordinates
nodes=struct('x',coords(:,1),... %# Assign x coordinates
'y',coords(:,2)); %# Assign y coordinates
plot([nodes.x],[nodes.y],'r*'); %# Plot all the nodes in red
index = randi(N,[1 2]) %# Pick two nodes at random
hold on;
plot([nodes(index).x],[nodes(index).y],'b*'); %# Plot 2 random nodes in blue
index(1) %# which node is selected first.
index(2) %# which node is selected second.
this question is follow up of this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要为每个节点分配“能量”值,可以修改 我之前的答案:
您可以使用MAX 函数:
您可以通过以下方式找到一对节点之间的距离:
If you want to assign a value for "energy" to every node, you can modify the code from my previous answer:
You can find the node with maximum energy using the MAX function:
and you can find the distance between a pair of nodes in the following way: