brini - if o1 and o2 are the x and y coordinates respectively of your five points, then your distance calculations can simplify to
d = zeros(5,5);
for i=1:5
for j=i+1:5
d(i,j) = sqrt((o1(i)-o1(j))^2 + (o2(i)-o2(j))^2);
d(j,i) = d(i,j);
end
end
Note how we pre-size the distance d matrix to be 5x5. We then iterate over each point calculating the distance between the two, (o1(i),o2(i)) and (o1(j),o2(j)). This is different from your distance calculation of
d(i,j)=sqrt([o1(i+1)-o1(i)].^2 + [o2(j+1)-o2(j)].^2)
which doesn't use the j value correctly when doing the subtraction. We subtract the x-components as o1(i)-o1(j) and the y-components as o2(i)-o2(j) before squaring each and then adding the two together. Since the distance between point i and j is the same as the distance between point j and i, then we can set
which saves us from doing a redundant calculation.
Now, when you want to draw a line between two points you need to use both the x and y components of those points. The code then becomes
for i=1:5
for j=i+1:5
if d(i,j)<=300
line([o1(i) o1(j)],[o2(i) o2(j)]);
end
end
end
Try the above and see what happens!
Best Answer