Using a for loop to reach a certain criteria

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

Answers (1)

You can use recursive functions instead of for loop with depth first search or breadth first search to achieve your requirement.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 12 Jun 2017

Community Treasure Hunt

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

Start Hunting!