2009-12-13 29 views
-2

作为帮助gnovice我得到了下面的代码,但现在我想分配能量(随机)到每个节点使用E=floor(rand(1)*10),也想比较最大能量和它们之间的距离是多少?如何将随机能量E分配给每个节点并比较两个节点的最大能量并找出它们之间的距离?

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

+1

如果您提到问题是后续问题,而不是仅仅提到后续问题,那么您可以帮助所有人回答您的问题。它是http://stackoverflow.com/questions/1749888/how-do-i-compare-elements-of-one-row-with-every-other-row-in-the-same-matrix-in-m? – 2009-12-13 16:56:53

+0

对不起,我的朋友,从下次我会照顾它。 – gurwinder 2009-12-13 17:37:57

回答

1

如果您想为“节能”的值分配给每一个节点,你可以从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 

您可以使用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); 
+0

再次感谢gnovice,非常感谢我的朋友.... – gurwinder 2009-12-13 17:51:27

相关问题