Finding Minimum Distance between two points

25 ビュー (過去 30 日間)
Flash747
Flash747 2016 年 10 月 27 日
コメント済み: Image Analyst 2019 年 7 月 7 日
Hi, I am trying to make a function to find minimum distance between my random points and a point (0,0) and plot the distance as a line crossing from the (0,0) to the one of the closest rand pt.
A=rand(2,10);
x1=A(1,:);
y1=A(2,:);
scatter(x1,y1);
[D I] = pdist2(x1,y1,'euclidean')
I have been trying to use 'pdist2' with 'euclidean' function however, it is not working well. :O
  1 件のコメント
KSSV
KSSV 2016 年 10 月 27 日
Dear friend..there will be only fixed distance between two points. Think of your question once.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2016 年 10 月 27 日
You don't need pdist2() because you aren't asking for the distance of every point to every other point. You're only asking for the distance from every point to the single point at (0, 0). So you can simply use sqrt()! Try this:
% Create 10 random points.
xy = rand(10, 2);
x = xy(:, 1);
y = xy(:, 2);
plot(x, y, 'bo', 'MarkerSize', 10);
grid on;
% Compute the distance of each of those points from (0, 0)
distances = sqrt(xy(: , 1) .^ 2 + xy(:, 2) .^ 2)
% Find the closest one.
[minDistance, indexOfMin] = min(distances);
closestX = x(indexOfMin);
closestY = y(indexOfMin);
% Mark it with red *
hold on; % Don't blow away existing points.
plot(closestX, closestY, 'r*', 'MarkerSize', 8, 'LineWidth', 2);
% Draw a line from the closest point to (0, 0)
line([0, closestX], [0, closestY], 'LineWidth', 2, 'Color', 'r');
  4 件のコメント
Paul Simon
Paul Simon 2019 年 7 月 7 日
Can you show the code for it?
Image Analyst
Image Analyst 2019 年 7 月 7 日
Paul, did you try it yourself using my directions above? I'm sure you did, and you probably got something like the code below:
% Program to make a set of random points and travel from (0,0) to
% the closest other remaining point.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
% Create 10 random points.
numPoints = 10;
xy = rand(numPoints, 2);
x = xy(:, 1);
y = xy(:, 2);
plot(x, y, 'bo', 'MarkerSize', 10);
grid on;
% First do it once to get the first point.
% Compute the distance of each of those points from (0, 0)
distances = sqrt(xy(: , 1) .^ 2 + xy(:, 2) .^ 2)
% Find the closest one.
[minDistance, indexOfMin] = min(distances);
closestX = x(indexOfMin);
closestY = y(indexOfMin);
% Mark it with red *
hold on; % Don't blow away existing points.
plot(closestX, closestY, 'r*', 'MarkerSize', 8, 'LineWidth', 2);
% Draw a line from the closest point to (0, 0)
line([0, closestX], [0, closestY], 'LineWidth', 2, 'Color', 'r');
% Now do it for the remaining points.
% Go from that point to the closest of the remaining other points.
alreadyUsed = false(1, numPoints);
counter = 2;
maxIterations = numPoints; % Failsafe
alreadyUsed(indexOfMin) = true; % Mark that point as having been used already.
while sum(alreadyUsed) <= (numPoints - 1) && (counter <= maxIterations)
% Compute the distance of each of those points from the last point.
lastx = x(indexOfMin(counter - 1));
lasty = y(indexOfMin(counter - 1));
% Set the already used distances to infinity so we won't consider them.
x(alreadyUsed) = inf;
% Compute distances from last point to all other points.
distances = sqrt((x - lastx) .^ 2 + (y - lasty) .^ 2);
[minDistance, indexOfMin(counter)] = min(distances);
% Find out the x and y of the closest point.
closestX = x(indexOfMin(counter));
closestY = y(indexOfMin(counter));
% Draw a line from the closest point to (0, 0)
line([lastx, closestX], [lasty, closestY], 'LineWidth', 2, 'Color', 'r');
% Mark this point as having already been used.
alreadyUsed(indexOfMin(counter)) = true;
counter = counter + 1;
end
0000 Screenshot.png
Note that this will not always or necessarily give you the shortest path length, like solving the traveling salesman problem will.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by