how to accumulate results from if, end

1 次查看(过去 30 天)
Dear all,
I'm checking points inside rectangle and I got result 1 for every point inside
, I want to accumulate this result but I don't no how to do it. could anyone help me. (i) is the points inside and the result is(PointsInside=1), my code is down
for m=1:11
AB=((XY(1,1)-XY(1,2))*(yy(:,m)-XY(2,2))-(XY(2,1)-XY(2,2))*(xx(:,m)-XY(1,2))>=0);
BC=((XY(1,2)-XY(1,3))*(yy(:,m)-XY(2,3))-(XY(2,2)-XY(2,3))*(xx(:,m)-XY(1,3))>=0);
CD=((XY(1,3)-XY(1,4))*(yy(:,m)-XY(2,4))-(XY(2,3)-XY(2,4))*(xx(:,m)-XY(1,4))>=0);
DA=((XY(1,4)-XY(1,1))*(yy(:,m)-XY(2,1))-(XY(2,4)-XY(2,1))*(xx(:,m)-XY(1,1))>=0);
if (AB||BC||CD||DA)==0
PointsInside=1
end
end
I hope somebody will help me
thanks

采纳的回答

Geoff Hayes
Geoff Hayes 2014-10-6
imola - just define an empty array outside of your for loop, and add the point that is inside the rectangle to this array whenever it satisfies the condition. Something like
pointsInRect = [];
for k=1:11
AB=((XY(1,1)-XY(1,2))*(yy(:,k)-XY(2,2))-(XY(2,1)-XY(2,2))*(xx(:,k)-XY(1,2))>=0);
BC=((XY(1,2)-XY(1,3))*(yy(:,k)-XY(2,3))-(XY(2,2)-XY(2,3))*(xx(:,k)-XY(1,3))>=0);
CD=((XY(1,3)-XY(1,4))*(yy(:,k)-XY(2,4))-(XY(2,3)-XY(2,4))*(xx(:,k)-XY(1,4))>=0);
DA=((XY(1,4)-XY(1,1))*(yy(:,k)-XY(2,1))-(XY(2,4)-XY(2,1))*(xx(:,k)-XY(1,1))>=0);
if (AB||BC||CD||DA)==0
pointsInRect = [pointsInRect ; xx(1,k) yy(1,k)];
end
end
As for your conditions, you are saying that if all are false then the point must be in the rectangle. Is that correct?
  1 个评论
imola
imola 2014-10-6
Dear Geoff,
Thank you very much you always help me I really appreciate that, that's great when I check now 8 points where inside I got them but I need to get the number of them, I will work on it and if I couldn't I might ask you.
Regards

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by