196 views (last 30 days)

Hi guys, Urgently need some help on this. I have made a code that analyses any given image and outputs the coordinates of the boundary points into 2 separate arrays, x and y. I want to find the distance between all the coordinates with each other. And then identify the coordinates that give the longest distance.

Thanks

sixwwwwww
on 20 Oct 2013

Dear Prabs, here is an example code which you can use for this purpose:

x = 1:10; % x component of coordinate

y = 2:2:20; % y component of coordinate

count = 1;

for i = 1:length(x) - 1

for j = i + 1:length(x)

distance(count) = sqrt((x(i) - x(j))^2 + (y(i) - y(j))^2);

Matrix(count, :) = [x(i) y(i) x(j) y(j) distance(count)];

count = count + 1;

end

end

SortedMatrix = sortrows(Matrix, 5);

MaxDis = SortedMatrix(size(Matrix, 1), :);

fprintf('Coordinates of maximum distnace are (x1, y1) = (%d, %d) and (x2, y2) = (%d, %d)\n', MaxDis(1), MaxDis(2), MaxDis(3), MaxDis(4))

fprintf('Maximum distance = %d\n', MaxDis(5))

I hope it helps.

Image Analyst
on 20 Oct 2013

Image Analyst
on 20 Oct 2013

Edited: Image Analyst
on 20 Oct 2013

You can find the distances from a point to all the other points in one single line, assuming you have the x and y coordinates. (Don't be afraid - the main code is only 4 lines long and is at the center between the ----- lines. The rest of it is just to make it fancy by displaying and printing the points.)

close all; % Close all figures (except those of imtool.)

clear; % Erase all existing variables. Or clearvars if you want.

clc; % Clear the command window.

workspace; % Make sure the workspace panel is showing.

% Initialization code

numberOfCoords = 20;

% Plot the points so we can see them.

x = rand(1, numberOfCoords);

y = rand(1, numberOfCoords);

plot(x, y, 'b*');

hold on;

for k = 1 : numberOfCoords

% Label the kth point in the plot.

text(x(k)+0.01, y(k), num2str(k));

end

maxDistance = zeros(1, numberOfCoords);

indexOfMax = zeros(1, numberOfCoords, 'int32');

%-----------------------------------------------

% Main engine:

% Find the furthest away points.

for k = 1 : numberOfCoords

distances = sqrt((x-x(k)).^2 + (y-y(k)).^2);

[maxDistance(k), indexOfMax(k)] = max(distances);

end

%-----------------------------------------------

% Done! Now show out results in the command window.

% Display in command window.

for k = 1 : numberOfCoords

thisDistance = maxDistance(k);

thisIndex = indexOfMax(k);

fprintf('Point #%d at (%.1f, %.1f) is farthest away from point #%d at (%.1f, %.1f) and is at a distance of %f\n',...

thisIndex, x(thisIndex), y(thisIndex),...

k, x(k), y(k), thisDistance);

end

Alaa
on 4 Mar 2015

Please, I have the same problem here:

And your help will be highly appreciated...

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/90794-finding-max-distance-between-coordinates#comment_175322

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/90794-finding-max-distance-between-coordinates#comment_175322

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/90794-finding-max-distance-between-coordinates#comment_175325

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/90794-finding-max-distance-between-coordinates#comment_175325

Sign in to comment.