Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-10, 16:49   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何为每个节点分配随机能量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. 这个问题是遵循了这个问题


回答:
如果要为每个节点分配“能量”值,则可以修改我先前的回答中的代码:

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);

更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 01:16


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.