我使用以下代码
在一个区域内分发了100个随机点(节点)n=100; xm=100;ym=100;
for i = 1 : 1 :n
S(i).xd = rand(1,1)*xm ;
S(i).yd = rand (1,1)*ym ;
xd(i) = S(i).xd ;
yd(i) = S(i).yd ;
S(i).id = i;
S(i).type = 0;
S(i).g = 0 ;
S(i).E = Eo ;
S(i).type = 0 ;
end
接下来,我使用以下代码
修复了区域边缘的10个网关节点 for i=1:1:10
Sg(i).xd= 2+100*rand(1,1);
Sg(i).yd=100;
xd(i)=Sg(i).xd;
yd(i)=Sg(i).yd;
Sg(i).id=i;
plot(Sg(i).xd,Sg(i).yd,'*k')
grid on;
hold on;
end
现在我已经使用100个节点的LEACH协议形成了簇头。
我必须从CH找到最小距离网关节点。由于有10个网关节点,我必须找到哪个更靠近该区域的CH。
确定CH我使用了以下代码
for all nodes in the area
temp_rand1=rand;
if(temp_rand1<= (p/(1-p*mod(r,round(1/p)))))
countCHs1=countCHs1+1;
S3(i).type=1;
S3(i).g=round(1/p)-1;
C1(cluster1).xd=S3(i).xd;
C1(cluster1).yd=S3(i).yd;
**distance1=sqrt((S3(i).xd-(gw_node.x) )^2 + (S3(i).yd-(gw_node.y) )^2 );**
C1(cluster1).distance1=distance1;
C1(cluster1).id=i;
X(cluster1)=S3(i).xd;
Y(cluster1)=S3(i).yd;
cluster1=cluster1+1;
end
我知道如何确定CH和一个网关节点之间的距离,但我不知道如何从一组网关节点中找到closet网关节点。 提前致谢
请回复
答案 0 :(得分:0)
不太确定你要求的是什么,但我想你想找到两个最接近的点。
这就是你能做到的。
%// Generate some points
n = 3; xm = 1:n; ym =1:n;
m = [xm' ym']; %// This is the line
xd = [ 2 8 3];
yd = [ 1 4 3];
d = [xd' yd']; %// These are the random points
drep = repmat(d,n,1); %// This will repeat the matrix d n times
B= ones(n,1);
mrep = kron(m,B); %// This will repeat each value in m n times
distances = bsxfun(@minus,mrep,drep); %// The distance from each point to every other point
C_squared = sum(distances.^2,2)
[min_dist,idx] = min(sum(distances.^2,2)) %// Find the minimum distance
value_in_m = mrep(idx,:)
ans =
3 3
value_in_d = drep(idx,:)
ans =
3 3
% Generate some points
n = 3;
xm = 1:n;
ym =1:n;
m = [xm' ym']; %// This is the line. Each row is a point
xd = [ 2 8 3];
yd = [ 1 4 3];
d = [xd' yd']; %// These are the random points
我们想要计算从一个点到每个点的距离
其他一点,所以我们重复点nxn
次
以下只是获取重复元素的一种方法。 更多建议如下:https://stackoverflow.com/a/16269185
drep = repmat(d,n,1); %// This will repeat the matrix d n times
B= ones(n,1);
mrep = kron(m,B); %// This will repeat each value in m n times
我们基于毕达哥拉斯定理,(m_x-d_x)^2 + (m_y-d_y)^2 = C^2
distances = bsxfun(@minus,mrep,drep); %// The distance from each point to every other point
C_squared = sum(distances.^2,2)
找到最小距离
[min_dist,idx] = min(sum(distances.^2,2));
找到距离最小的点
value_in_m = mrep(idx,:)
ans =
3 3
value_in_d = drep(idx,:)
ans =
3 3
答案 1 :(得分:0)
不确定你在问什么。我假设你有一组固定点在一条直线上(标记为蓝色)和一组随机点(标记为红色)。你想要一个固定点和一个随机点之间的最小距离。
试试这段代码:
si = randi([0,100],10,2); %// Random points (red)
sg(:,1) = randi([0,100],10,1); %// fixed points x param (blue)
sg(:,2) = ones(10,1)*50; %// fixed points y param (blue) r const, to make them a st. line
sgc = mat2cell(sg,ones(1,size(sg,1)),size(sg,2)); %// converting to cell array
sic = mat2cell(si,ones(1,size(si,1)),size(si,2)); %// converting to cell array
%// distMat gives n*n matrix of all the dist between each fixed point with every other point.
distMat = cellfun(@(x,y) pdist2(x,y),repmat(sic,1,size(sgc,1)),repmat(sgc',size(sic,1),1));
figure; axis('image');
plot(si(:,1),si(:,2),'r.');
text(si(:,1),si(:,2),[repmat(' ',size(si,1),1), num2str((1:size(si,1))')]);
hold on
plot(sg(:,1),sg(:,2),'b.');
text(sg(:,1),sg(:,2),[repmat(' ',size(sg,1),1), num2str((1:size(sg,1))')]);
hold off
[mdist, index] = min(distMat(:));
[randomP, fixedP] = ind2sub(size(distMat),index)
简介:
输出:
randomP =
2
fixedP =
2
>> mdist
mdist =
6.7082