-2

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.

Community
  • 1
  • 1
gurwinder
  • 237
  • 1
  • 5
  • 13
  • 1
    You would help everyone answer your question if you mentioned the question it is a follow-up to instead of just mentioning it is a follow-up. Is it http://stackoverflow.com/questions/1749888/how-do-i-compare-elements-of-one-row-with-every-other-row-in-the-same-matrix-in-m ? – Pascal Cuoq Dec 13 '09 at 16:56
  • i am sorry my friend, from next time i'll take care of it. – gurwinder Dec 13 '09 at 17:37

1 Answers1

1

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);
Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359