how to accumulate results from if, end

1 view (last 30 days)
imola
imola on 6 Oct 2014
Edited: imola on 11 Oct 2014
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

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Oct 2014
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 Comment
imola
imola on 6 Oct 2014
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!