How can we provide minimum distance i.e. 20 between each random generated node?
5 次查看(过去 30 天)
显示 更早的评论
No_of_clusters = 25
no_sensor_box = 8
for i=1:no_of_clusters
for j=1:no_sensor_box
Xloc(s)=randi([x_loc1(i)+1,x_loc2(i)-1],1,1);
Yloc(s)=randi(([y_loc1(i)+1,y_loc3(i)]-1),1,1);
s=s+1;
end
end
This code generates nodes at some random locations, but I want the minimum distance (i.e.=20) between two nodes that means the minimum distance between each node are not less than 20. How to provide minimum distance between two nodes in this?
1 个评论
Torsten
2017-11-8
How is the distance between two nodes s1 and s2 defined ?
d(s1,s2) = sqrt((Xloc(s1)-Xloc(s2))^2+(Yloc(s1)-Yloc(s2))^2)
?
Best wishes
Torsten.
采纳的回答
John D'Errico
2017-11-8
Um, there isn't a way. Well, not to do it by one simple call. You will need to write code.
Given the set of nodes already generated, just generate a new random point, subject to no constraints. Use a uniform random generator, like rand. Compute the distance to every other node. This is trivial of course.
Then if the new node is too close to any other node, toss it away, into the bit bucket. Repeat until you find a new node that is acceptable.
Eventually, you will find that it becomes really difficult to choose new nodes. You are done then.
Sorry, but that is what you will need to do. Could you do it more efficiently? Well, yes. I suppose in theory, you could do so. You would need to build a triangulation of the region that would define the region where new nodes may be validly chosen, because they are sufficiently faraway from any existing node. Then choose a new point uniformly from the valid domain. Given the new node, since it was in the valid set, exclude all portions of the existing triangulation that are within a radius of r from the new node, reducing the valid set.
This may become somewhat computationally intensive to do the exclusion, because that triangulation may become a rather messy one. But it would get smaller all the time too.
So, COULD you solve your problem without using a simpler rejection scheme? Yes. But not worth the effort unless you really understand how to work with triangulations of the plane. Just use the first scheme I proposed.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!