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
0 个评论
采纳的回答
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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!