I have used the following code to create a wsn structure wth 4*4 cell grids and have randomly deployed 100 nodes in it.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Parveen verma
am 10 Jun. 2015
Kommentiert: Parveen verma
am 11 Jun. 2015
I have used the following code to create a wsn structure with 4*4 cell grids and have deployed randomly 100 nodes in it.Now i want to elect cell-headers with the criteria that the node closest to mid-point of the cell is elected as cell-header. How to do it? Please help
NrGrid = 4; % Number Of Grids
x = linspace(0, 100, NrGrid+1);
[X,Y] = meshgrid(x);
figure(1)
plot(X,Y,'k')
hold on
plot(Y,X,'k')
hold off
set(gca, 'Box','off', 'XTick',[], 'YTick',[])
axis square
NrGrid = 4;
coords = rand(100,2) * maNrGrid + 1;
scatter(coords(:,1),coords(:,2));
set(gca, 'XTick', 1:maNrGrid+1, 'YTick', 1:maNrGrid+1)
grid on
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 10 Jun. 2015
[XC, YC] = ndgrid(1/2:NrGrid+1/2, 1/2:NrGrid+1/2);
center_idx = zeros(NrGrid, NrGrid);
for K = 1 : numel(XC)
xc = XC(K);
yc = YC(K);
dists = sqrt((coords(:,1)-xc).^2 + (coords(:,2)-yc).^2);
[mindist, minidx] = min(dists);
center_idx(K) = minidx;
end
Now center_idx will be a 4 x 4 array where each entry gives the row number in coords() of the node that is the closest to the center of the cell. For example if the entry at (2,4) says 73 then coords(73,:) would be the node closest to the center of the grid that goes 2..3 in X and 4..5 in Y.
3 Kommentare
Walter Roberson
am 11 Jun. 2015
No, see the "for K" loop? One cluster-head is chosen for each K, and there are 16 locations, so 16 cluster-heads will be chosen.
Note: the cluster-head chosen is not necessarily going to be in the cell. Consider
|-------|-------
|1......|.......
|.......|.......
|.......|.......
|...0...|2......
|.......|.......
|.......|.......
|.......|.......
|-------|-------
Distance 0 to 1 is 3*sqrt(2) = 4.24 and distance 0 to 2 is 4, so node 2 is closer to the center of the left grid than node 1 is, and therefore node 2 would be selected instead of node 1.
When the population of nodes is high then it is statistically unlikely to happen, but it is a possibility.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!