what white arrows means in debugger?
How I finish a recursive function?
26 views (last 30 days)
Show older comments
Alejandro Fernández Guerrero
on 3 Sep 2020
Commented: Alejandro Fernández Guerrero
on 3 Sep 2020
Hello,
I am really new in matlab programming and I wanted to finish my recursive fuction called partition,
Is a function how find contact points with some conditions implemented in existCP function.
I get the solution of my problen when c(1) == b(1) (STOP CONDITION), but when it should left the partition funcion and export CP as resullt in Recursive function it doesn´t work.
I mean when I run Partition in debugger, the green arrow goes out of partition function but then instead of finish Recursive function the green arrow comes again in Particion(P,X,r,c,b,puntos).
function [CP]=Recursive(P,s,r)
n=length(P);
X(1)=0;
for jj = 1:n-1
X(jj+1)=X(jj)+s;
end
a=[X(1),P(1)];
b=[X(end),P(end)];
points=a;
CP=Particion(P,X,r,a,b,points);
end
function [CP] = Particion(P,X,r,a,b,points)
while 1
[flag,c]=existCP(P,X,r,a,b);
if flag==true
Particion(P,X,r,a,c,points);
else
b=[X(end),P(end)];
if c(1) == b(1)
CP=points;
return
else
points = [points;c];
Particion(P,X,r,c,b,points);
end
end
end
end
Answers (1)
Jon
on 3 Sep 2020
In your recursive function there must finally be a branch where you encounter a return.
Once it hits this return, it will back its way up the stack until it finally exits.
If your code never terminates, or hits recursion limit, then somehow your logic must not provide a path for the calculation to finally reach the return statement.
I think the white arrows show where it left the current code and went to the next recursion level.
2 Comments
Jon
on 3 Sep 2020
If I want to try running your code, what values of P,s and r should I use to demonstrate the issue you are having?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!