-2

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.

この質問は、この質問のフォローアップです。

4

1 に答える 1

1

すべてのノードに「エネルギー」の値を割り当てたい場合は、前の回答のコードを変更できます。

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);
于 2009-12-13T17:45:48.980 に答える