如何为每个节点分配随机能量 E 并比较两个节点的最大能量并找到它们之间的距离?

发布于 2024-08-14 18:23:10 字数 917 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2024-08-21 18:23:10

如果要为每个节点分配“能量”值,可以修改 我之前的答案

N = input('no. of nodes : ');      %# Number of nodes
coords = num2cell(rand(N,2));      %# Cell array of random x and y coordinates
E = num2cell(ceil(10*rand(N,1)));  %# Cell array of random integers from 1 to 10
nodes = struct('x',coords(:,1),...   %# Assign x coordinates
               'y',coords(:,2),...   %# Assign y coordinates
               'energy',E);          %# Assign energy

您可以使用MAX 函数:

[maxValue,maxIndex] = max([nodes.energy]);

您可以通过以下方式找到一对节点之间的距离:

index = [1 3];  %# Compare nodes 1 and 3
dist = sqrt(diff([nodes(index).x])^2+diff([nodes(index).y])^2);

If you want to assign a value for "energy" to every node, you can modify the code from my previous answer:

N = input('no. of nodes : ');      %# Number of nodes
coords = num2cell(rand(N,2));      %# Cell array of random x and y coordinates
E = num2cell(ceil(10*rand(N,1)));  %# Cell array of random integers from 1 to 10
nodes = struct('x',coords(:,1),...   %# Assign x coordinates
               'y',coords(:,2),...   %# Assign y coordinates
               'energy',E);          %# Assign energy

You can find the node with maximum energy using the MAX function:

[maxValue,maxIndex] = max([nodes.energy]);

and you can find the distance between a pair of nodes in the following way:

index = [1 3];  %# Compare nodes 1 and 3
dist = sqrt(diff([nodes(index).x])^2+diff([nodes(index).y])^2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文