Finding Max Distance between coordinates
37 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
2 comentarios
Respuestas (3)
sixwwwwww
el 20 de Oct. de 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.
1 comentario
Image Analyst
el 20 de Oct. de 2013
You can use a vectorized approach, like I did in the engine at the middle of my code below to have a more "MATLAB-ish" faster, more efficient approach.
Image Analyst
el 20 de Oct. de 2013
Editada: Image Analyst
el 20 de Oct. de 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
0 comentarios
Alaa
el 4 de Mzo. de 2015
Please, I have the same problem here:
And your help will be highly appreciated...
0 comentarios
Ver también
Categorías
Más información sobre Feature Detection and Extraction en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!