www.gusucode.com > UWB_matlab源码程序 > CP1002/cp1002_create_network.m

    % 
% FUNCTION 10.1 : "cp1002_create_network"
%
% This function generates the positions of a set of nodes
% in a square area and computes the distance between each
% pair of nodes
% The function receives in input:
% - The number of nodes N
% - The sidelength of the square area
% - A flag G to enable/disable the graphical output
% The function returns:
% - A matrix Nx2 containing the (X,Y) positions of each
%   node
% - A matrix NxN containing the distances between each pair
%   of nodes
%
% Programmed by Luca De Nardis
%

function [positions, ranges] = ...
   cp1002_create_network(N,area_side,G)
for i = 1:N
    positions(i,1)=rand*area_side;
    positions(i,2)=rand*area_side;
    j=1;
    for j=1:(i-1)
        ranges(i,j)= sqrt((positions(i,1) -...
           positions(j,1))^2 + (positions(i,2) -...
           positions(j,2))^2);
        while(ranges(i,j)==0)
                X(i)=rand*50;
                Y(i)=rand*50;
                ranges(i,j)=sqrt((positions(i,1)-...
                   positions(j,1))^2+(positions(i,2)-...
                   positions(j,2))^2);
        end
        ranges(j,i)=ranges(i,j);
    end
end
if G
    scatter(positions(:,1),positions(:,2),'filled');
    axis([0 area_side 0 area_side]);
    xlabel('X [m]');
    ylabel('Y [m]');
    box on;
end