フィルターのクリア

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 Center および File ExchangeWSNs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by