필터 지우기
필터 지우기

Creating a loop to find next point in array

조회 수: 3 (최근 30일)
Joe Cousins
Joe Cousins 2021년 8월 27일
댓글: Joe Cousins 2021년 8월 27일
function [next_point, distance] = find_next_point(current_point, points_to_check, x, y)
distance = compute_distance(current_point, points_to_check, x, y) %find all distances in array
[distance, idx] = min(distance) %find smallest of these distances
next_point = points_to_check(idx)
end
If current_point = [1] and points_to_check = [2 3 4 5 6 7 8 9 10 11 12] how can I turn this code into a for loop so that it determines the smallest distance to the next point (taken from points_to_check) for each iteration?
e.g. after the first iteration current_point = [4] and points_to check = [2 3 5 6 7 8 9 10 11 12] so each iteration the current point is removed from points_to_check and the current_point changes depending on which is the closest point.

채택된 답변

Kevin Holly
Kevin Holly 2021년 8월 27일
편집: Kevin Holly 2021년 8월 27일
Edit: I just realized I misread what you wanted. I thought you needed to remove the distance, not current_point.
Assuming index for distance is same as index for points_to_check. Let me know if this works.
function [next_point, distance] = find_next_point(current_point, points_to_check, x, y)
next_point = [];
for i = length(points_to_check):-1:1
distance = compute_distance(current_point, points_to_check, x, y); %find all distances in array
[~, idx] = min(distance); %find smallest of these distances
%save array of answers
next_point = [next_point points_to_check(idx)];
%Update parameters for next run
current_point = points_to_check(idx);
points_to_check(idx) = [];
end
end
  댓글 수: 1
Joe Cousins
Joe Cousins 2021년 8월 27일
That works perfectly! Thanks for the help

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Breaks, Knots, and Sites에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by