Use a for loop to find out if point B is away from point A by a known amount
2 次查看(过去 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!