Use a for loop to find out if point B is away from point A by a known amount
조회 수: 1 (최근 30일)
이전 댓글 표시
I have point A and point B. (In the figure only two pairs of A and B.)
condition: I want to find out if point B is far from point A by an amount between -2 and 2 in the X and Y direction (the -2 and 2 are included).
I need to create a for loop for all points (all rows) of the "sortedmat" matrix so that:
- if the condition is verified, I have to redo the loop.
- if the condition is not verified, I have to save the array with the points that were within the two intervals.
I started in the following way but I don't know how to go about it.
sortedmat = importdata("sortedmat.txt");
positive_distance_X = 2;
negative_distance_X = -2;
positive_distance_Y = 2;
negative_distance_Y = -2;
row_used_A = 1;
% row_used_A = 309;
point_A_direction_X = sortedmat(row_used_A,1);
point_A_direction_Y = sortedmat(row_used_A,2);
row_used_B = row_used_A + 1;
point_B_direction_X = sortedmat(row_used_B,1);
point_B_direction_Y = sortedmat(row_used_B,2);
distance_X_direction = point_B_direction_X - point_A_direction_X; % must be between negative_distance_X & positive_distance_X
distance_Y_direction = point_B_direction_Y - point_A_direction_Y; % must be between negative_distance_Y & positive_distance_Y
댓글 수: 0
채택된 답변
Karim
2022년 12월 26일
Hi see below for a demonstration of such a loop.
% load the data
sortedmat = importdata("sortedmat.txt");
% set the allowed delta
delta = 2;
% figure out the number of points
numPoints = size(sortedmat,1);
for i = 1:(numPoints-1)
% get the points we are now looking at
Point_A = sortedmat(i ,:);
Point_B = sortedmat(i+1,:);
% check the condition
MyCondition = Point_A(1) >= Point_B(1)-delta && Point_A(1) <= Point_B(1)+delta && ...
Point_A(2) >= Point_B(2)-delta && Point_A(2) <= Point_B(2)+delta;
% if the condition is false, stop the loop
if ~MyCondition
break
end
end
% get some info about the result
disp("The first "+num2str(i)+" consecutive points are within a delta of "+num2str(delta)+" pixels")
figure
plot(sortedmat(1:i,1),sortedmat(1:i,2),'r')
grid on
title('consecutive points within delta')
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!