how to use "for loop" for firstly entering into each rectangular grids then find minimum distance b/w nodes in each grid

1 次查看(过去 30 天)
here i want to apply for loop for each 16 rectangular grids then after entering into the grid i want to find eucledian distance b/w the nodes present in that grid
  3 个评论
kiranpreet kaur
kiranpreet kaur 2016-8-25
sir,i knows how to find distance b/w each nodes but i don't know the syntax to enter inside each box and then find distance b/w the nodes present randomly in each box. for example i want to goto in first box then i have to apply minimum distance operation on the nodes present in first box.............for this procedure how could i use for loops plz sir tell me the nested for loop syntax for this procedue. sir plz help me with a syntax for my problem

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2016-8-26
close all;
clc;
ngrid=4;%no. of grids
N=200;
R=0.5;
sink.x=3.1;
sink.y=3.1;
x = linspace(0, 4, ngrid+1);
E=0.5;
[X,Y] = meshgrid(x);
figure(1)
plot(X,Y,'k')
hold on
plot(Y,X,'k')
hold on
ngrid = 4;
coords = rand(N,2) * ngrid;
nodes = struct('x',coords(:,1),... %# Assign x coordinates
'y',coords(:,2),... %# Assign y coordinates
'energy',E);
scatter(coords(:,1),coords(:,2));
set(gca, 'XTick', 1:ngrid+1, 'YTick', 1:ngrid+1)
axis square
plot(nodes.x,nodes.y,'o','LineWidth',2, 'MarkerEdgeColor','m','MarkerFaceColor','m','MarkerSize',3)
plot(sink.x,sink.y,'d','LineWidth',2, 'MarkerEdgeColor','r','MarkerFaceColor','y','MarkerSize',12)
grid on;
%%Get nodes inside for each box
P1 = cell(4,4) ;
for i = 1:4
for j = 1:4
A = [X(i,j) Y(i,j)] ;
B = [X(i+1,j+1) Y(i+1,j+1)] ;
idx = find(nodes.x >= A(1) & nodes.x <B(1)) ;
idy = find(nodes.y >= A(2) & nodes.y <B(2)) ;
id = intersect(idx,idy) ;
P1{i,j} = [nodes.x(id) nodes.y(id)] ;
% plot points inside first box
plot(P1{i,j}(:,1),P1{i,j}(:,2),'*g')
end
end
I have added a code to separate the points in each box.....P1 is a cell..it has all the points associated w.r.t box.
P{1,1} gives points inside first box.....P(3,2} gives points inside 3 row 2 column box....
Good luck
  1 个评论
kiranpreet kaur
kiranpreet kaur 2016-8-28
thanks for ur help sir,................ sir i want to find eucledian distance in P(3,2)...bcoz according to me when i find the minimum distance b/w the nodes it shows the minimum distance of nodes of whole network .............I just only wants to find the minimum distance in P(3,2).plz help me sir.....i would be very thankfull to u.........help me with a syntax

请先登录,再进行评论。

类别

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