Use a for loop to find out if point B is away from point A by a known amount
4 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Karim
on 26 Dec 2022
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 Comments
More Answers (0)
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!