필터 지우기
필터 지우기

how to accumulate results from if, end

조회 수: 1 (최근 30일)
imola
imola 2014년 10월 6일
편집: imola 2014년 10월 11일
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