compare elements of the same array

2 views (last 30 days)
I have a network with 10 nodes, each of which knows exactly the distance from other nodes. Suppose node 1 starts to transmit. All nodes at a distance greater than d from another node can transmit at the same time. Observing the adjacent array of node 1, we can see NODE(1).adj = [0 1 1 1 0 1 0 1 0 0]. It means that node 2, 3, 4, 6, 8 are at a distance greater than d from node 1 (if the distance is greater than d the value is 1). We put these nodes in another array called CANDIDATES, so CANDIDATES = [2 3 4 6 8]. But node 2 and node 6 cannot transmit at the same time because their mutual distance is lower than d, as we can see by analyzing NODE(2).adj. My idea is: the first element of CANDIDATES transmit, in this case node 2, then the second element, node 3, controls its distance from the active node, node 2: if the distance between node 2 and node 3 is greater than d then node 3 can transmit; after that, the third element, node 4, controls its distance from all active nodes, node 2 and node 3: if the distance from both nodes is greater than d, node 4 can transmit at the same time. In the test I've made only node 1, 2 and 3 can transmit simultaneously. how can I make this comparison? here is the code I've written so far
%%%%%%%%%%%%%%%%%%%%PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%field dimensions
xm = 50;
ym = 50;
%numeber of nodes
n = 10;
%treshold distance
d = 30;
%%%%%%%%%%%%%%%%%%%%END PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%
%create a network
for i=1:1:n
%node position
NODE(i).xd = rand(1,1)*xm;
NODE(i).yd = rand(1,1)*ym;
%adjacent array
NODE(i).adj = zeros(1,n);
end
for i=1:1:n
%node state: 1 tx/rx, 0 dozen
NODE(i).state(1,1) = 0;
%packet type: 1, 2, 3
NODE(i).pkt(1,1) = 0;
%destination
NODE(i).pkt(2,1) = 0;
end
%adjacency matrix: array which relates only to the node i; matrix built by rows
for i=1:1:n
for j=1:1:n
if(i ~= j)
distance = sqrt((NODE(i).xd - NODE(j).xd)^2+(NODE(i).yd - NODE(j).yd)^2);
NODE(i).adj(1,j) = distance;
end
end
end
%pick a random destination (random number between o and n)
dst = floor((n+1)*rand(1));
%a random node begin transmissions
temp = ceil(n*rand);
NODE(temp).state(1,1) = 1;
NODE(temp).pkt(1,1) = 1;
NODE(temp).pkt(2,1) = dst;
for i=1:1:n
if(NODE(temp).adj(1,i) > d)
provvisorio(1,i) = i;
else
provvisorio(1,i) = 0;
end
end
j = 1;
for i=1:1:n
if(provvisorio(1,i) ~= 0)
candidate(1,j) = provvisorio(1,i);
j = j+1;
end
end

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 28 Mar 2013
Edited: Andrei Bobrov on 28 Mar 2013
n = 10;
xm = 40;
ym = 60;
ij = [randi(xm,n,1), randi(ym,n,1)];
d = bsxfun(@minus,ij,permute(ij,[3,2,1]));
dist1 = squeeze(bsxfun(@hypot,d(:,1,:),d(:,2,:)));
dist1(1:n+2:end) = inf;
out = find(all(dist1 > 30));

More Answers (0)

Categories

Find more on Data Types in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!