Finding Max Distance between coordinates
조회 수: 45 (최근 30일)
이전 댓글 표시
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
답변 (3개)
sixwwwwww
2013년 10월 20일
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
Image Analyst
2013년 10월 20일
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
2013년 10월 20일
편집: Image Analyst
2013년 10월 20일
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
Alaa
2015년 3월 4일
Please, I have the same problem here:
And your help will be highly appreciated...
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 3-D Volumetric Image Processing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!