Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

compare elements of the same array

Asked by Roberto

Roberto

on 28 Mar 2013
Accepted Answer by Andrei Bobrov

Andrei Bobrov

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

0 Comments

Roberto

Roberto

Products

No products are associated with this question.

1 Answer

Answer by Andrei Bobrov

Andrei Bobrov

on 28 Mar 2013
Edited by Andrei Bobrov

Andrei Bobrov

on 28 Mar 2013
Accepted answer
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));

2 Comments

Andrei Bobrov

Andrei Bobrov

on 28 Mar 2013

corrected.

Roberto

Roberto

on 3 Apr 2013

thank you

Andrei Bobrov

Andrei Bobrov

Contact us