%% 隨機產生N個點
N = 5;
A = randint(N,2,[1 20]);
for p = 1:N
x = A(p,1);
y = A(p,2);
plot(x,y,"o");
hold on
text(x+0.5,y+0.5,num2str(p));
end
axis([0 30 0 30]);
axis equal
grid on
dd = pdist(A)";
mind = min(dd);
id = find(dd==mind);
ppp = [nchoosek(1:N,2) pdist(A)"];
for p = 1:length(id)
k = id(p);
ppp(k,:)
x1 = A(ppp(k,1),1);
y1 = A(ppp(k,1),2);
x2 = A(ppp(k,2),1);
y2 = A(ppp(k,2),2);
plot(x1,y1,"ro");
plot(x2,y2,"ro");
line([x1 x2],[y1 y2]);
text(x1+1,y1-1,["mind = " num2str(dd(k))]);
hold off
%% 隨機產生N個點
N = 5;
A = randint(N,2,[1 20]);
for p = 1:N
x = A(p,1);
y = A(p,2);
plot(x,y,"o");
hold on
text(x+0.5,y+0.5,num2str(p));
end
axis([0 30 0 30]);
axis equal
grid on
dd = pdist(A)";
mind = min(dd);
id = find(dd==mind);
ppp = [nchoosek(1:N,2) pdist(A)"];
for p = 1:length(id)
k = id(p);
ppp(k,:)
x1 = A(ppp(k,1),1);
y1 = A(ppp(k,1),2);
x2 = A(ppp(k,2),1);
y2 = A(ppp(k,2),2);
plot(x1,y1,"ro");
plot(x2,y2,"ro");
line([x1 x2],[y1 y2]);
text(x1+1,y1-1,["mind = " num2str(dd(k))]);
end
hold off