How would I write a function that finds the closest point from a list of coordinates?
이전 댓글 표시
I have to write a function that calculates the distance between multiple points and a base site, then finds the closest point to the base site. This is the format I have to follow:
function [chosen_site, d] = select_closest_site( current_site, site_list, x, y )
where x and y are vectors containing the appropriate coordinates for each point, site_list is a vector with the IDs of the points that will be compared, and current_site is the base site that the distances will be calculated from.
Here's an example of how the code should function:
Consider the list of four sites, with locations x = [3, 1, 2, 2] and y = [5, 2, 4, 6]. Given current_site = 4 and site_list = [1, 3], the function should consider which of sites 1 and 3 is closer to site 4, then select site 1 as it is closer. The function should also report the distance to that site, √2:
I also have to call a function that calculates distance which I have already written. This is what I have for that:
function d = compute_distances( x_c, y_c, x, y )
d = sqrt((x_c - x).^2 + (y_c - y).^2);
end
This is what I have written so far to call in the distance function:
d = compute_distances(x(current_site), y(current_site), x, y);
How would I go about continuing past this? I assume I will have to use the min function but I'm struggling to figure out how to format it.
Any help is appreciated.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!