Using a for loop to reach a certain criteria
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello
I'm trying to use a for loop to reach a certain criteria from a function. I have a function that breaks triangles down into four equilateral triangles. I want to stop that once a triangle has an area of 625m^2. However, I want to continue reducing the other triangles that haven't reached that criteria.
The actual equation for the criteria I'm using is
eta=abs(625-A(n))<abs(625-A(n+1))
The code I've written so far is below. It's very wrong but I don't really know how to go about getting what I want. Any advice is appreciated :)
[coords,trian,bound_code]=refine_m(G.lonlat,tri_1,1:length(tri_1),bound_code);
for ll=1:length(coords)
for ii=1:length(trian)
triX=coords(trian,1);
triY=coords(trian,2);
An(ii)=(1/2)*(((triX(ii,2)-triX(ii,1))*(triY(ii,3)-triY(ii,1)))-((triX(ii,3)-...
triX(ii,1))*(triY(ii,2)-triY(ii,1))));
if An(ii)>625
[coords,trian,bound_code]=refine_m(coords,trian,1:length(trian),bound_code);
end
triX=coords(trian,1);
triY=coords(trian,2);
An2(ii)=(1/2)*(((triX(ii,2)-triX(ii,1))*(triY(ii,3)-triY(ii,1)))-((triX(ii,3)-...
triX(ii,1))*(triY(ii,2)-triY(ii,1))));
if abs(625-An(ii))<abs(625-An2(ii))
coords(ll)=G.lonlat(ll);
trian(ii)=trian(ii);
bound_code(ll)=bound_code(ll);
elseif abs(625-An(ii))>abs(625-An2(ii))
[coords,trian,bound_code]=refine_m(coords,trian,1:length(trian),bound_code);
end
end
end
댓글 수: 0
답변 (1개)
Sanjana Ramakrishnan
2017년 6월 15일
You can use recursive functions instead of for loop with depth first search or breadth first search to achieve your requirement.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!