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 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2015-6-10
[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 个评论
Walter Roberson
Walter Roberson 2015-6-11
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.
Parveen verma
Parveen verma 2015-6-11
OK, that is clear that 16 nodes will be selected as cluster-heads but i want to highlight that the following nodes are selected as cluster-heads. Following is the pic. attached of the nodes in the grid when i run the code , but still the cluster-heads are not visible in it. Nodes and cluster-heads cannot be differentiated. How to do that? Please help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 WSNs 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by