I have randomly placed 10 nodes in a 100x100 area. Now I need those node to get connected to each other that are within the provided range.
4 views (last 30 days)
Show older comments
I am sending my entire code in this. Please check it File 1:
clc;
close all;
clear all;
x=randi(100,1,10)
y=randi(100,1,10)
save('anch','x','y')
File 2:
clc;
close all;
clear all;
load 'anch.mat'
Pop = [x ;y]
N_nodes=10;
[m n] = size(Pop);
Dist = zeros(m, n);
for f = 1 : N_nodes
for s = 1 : N_nodes
Dist(f, s) = sqrt((Pop(1, s) - Pop(1, f)).^ 2 + (Pop(2, s) - Pop(2, f)).^ 2);
save('Distance.mat','Dist');
end
end
File 3:
clc;
close all;
clear all;
load 'unknwn.mat'
load 'anch.mat'
load 'Distance.mat'
plot(x,y,'kd','MarkerSize',8,'lineWidth',2,'MarkerFaceColor','c');
axis([0,100,0,100])
grid on;
hold on;
plot(x1,y1,'bo','MarkerSize',8,'lineWidth',2,'MarkerFaceColor','m');
legend('Anchor nodes','Unknown nodes')
range =40;
%connecting the array according to the anchor nodes that lie in the
%provided range
for d=1:length(Dist)
for i=1:length(x)
if((i+1)<=length(x))
for j=(i+1):length(x)
if((j+1)<=length(x))
for(k=(j+1):length(x))
C=[x(k);y(k)]
B=[x(j);y(j)]
A=[x(i);y(i)]
if (Dist(A):Dist(B))<=range
line([x(i) x(j)],[y(i) y(j)]);
end
if (Dist(B):Dist(C))<=range
line([x(j) x(k)],[y(j) y(k)]);
end
if (Dist(C):Dist(A))<=range
line([x(k) x(i)],[y(k) y(i)]);
end
end
end
end
end
end
end
for i=1:10
text(x(i),y(i),sprintf('(%d,%d)',x(i),y(i)))
end
There are many nodes which has its neighboring nodes within the provided range but its not Connecting. Please help me out to solve
this problem.
4 Comments
Guillaume
on 9 Apr 2018
Edited: Guillaume
on 9 Apr 2018
Re: file2, as per the comment it needs R2016b or later. On earlier versions, it's
Dist = hypot(bsxfun(@minus, Pop.x, Pop.x'), bsxfun(@minus, Pop.y, Pop.y'));
Re: the rest. What does they are to be connected mean? A line is to be traced between them?
Answers (2)
KSSV
on 9 Apr 2018
x=randi(100,1,10) ;
y=randi(100,1,10) ;
dt = delaunayTriangulation(x',y') ;
triplot(dt)
0 Comments
Guillaume
on 9 Apr 2018
Edited: Guillaume
on 9 Apr 2018
File3:
Unknown = load('unknwn.mat');
Pop = load('anch.mat');
Dist = load('Distance.mat');
Dist = Dist.Dist;
figure;
plot(Pop.x, Pop.y, 'kd', 'MarkerSize', 8, 'lineWidth', 2, 'MarkerFaceColor', 'c');
axis([0,100,0,100])
grid on;
hold on;
plot(Unknown.x1, Unknown.y1, 'bo', 'MarkerSize', 8, 'lineWidth', 2, 'MarkerFaceColor', 'm');
legend('Anchor nodes','Unknown nodes')
range = 40;
[A, B] = find(triu(Dist < range, 1)); %only use upper triangle to avoid diagonal (where dist is 0) and symmetric lower triangle
line(Pop.x([A; B]), Pop.y([A; B]))
Notes:
- Clearly you overuse loops when vectorising is much faster and simpler
- I'm loading your mat files into variables rather than just popping variables into the workspace. I recommend you do the same. It's a lot safer.
8 Comments
MATLAB feng
on 29 May 2022
Hello, may I ask, I need to pass an S curve in a square area, and then establish an anchor node at the distance of each corner. How should I build it, I hope to give some advice
See Also
Categories
Find more on Time Series Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!