Return function in a recursive function code

When I create a recursive code and have the desired output, after following it with 'return' the next line - it continues running and outputting unwanted results - can someone help me avoid this please.

17 个评论

Can you show us your friends function code, along with data you are using to call that function?
[C]=Function(A,P,K)
%A is adjacency matrix of a Graph say
%P, K are vectors
if (terminating condition)
   C=P;
   return
else 
    ......
    P=.....%redefined
    K=.....%redefined
    Function(A,P,K)
end
end

The code is of this type.

I think in the first condition you meant K=P. The only reason it can be happening is C never becomes equal to P.

No, the termination condition could be that all(P)==1 for instance and this is reached, I've debugged it and seen every step. I can use disp(P) but this doesn't output the result I want as a matrix.

The line

    Function(A,P,K)

does not change C, so there is no point running the call.

true, C is only determined right at the end, can I display the output as a matrix using disp(..)

If you want to return P from the last termination call to recursive function than you will need to assign it to the output of Function in else condition like this

[C]=Function(A,P,K)
%A is adjacency matrix of a Graph say
%P, K are vectors
if (terminating condition)
   C=P;
   return
else 
    ......
    P=.....%redefined
    K=.....%redefined
    C = Function(A,P,K);
end
end

No point of using disp if you want the output in a matrix.

i see my mistake, thanks
I'm actually facing a similar problem today again
[]=function(A,b,c,i,j,f,G)
if termination condition
disp(G) %as G is a matrix containing matrices, I'm sending it to my workspace as a variable at the moment
return
else
if some condition
G{i,j}= some matrix
else
....
end
end
if true
% code
end
The issue is this function runs even after I've sent that big G I'm building up I presume because of the recursive nature. I can't really use the idea above as I'm using my function to find an entry via several steps. Any help will be appreciated as after debugging I see the out of memory error is coming after when I would like the algo to stop - despite the use of return.
There is no recursion in above code. It is hard to spot an error if we cannot see where the recursion is happening.
function[]=HELP(A,R,P,X,i,j,n,D,C)
%A is a fixed matrix, R is something I'm building up then assigning it as an try of C, P, X are matrices for the algo, i and j are indices for the entry in C, n and D are used for the algo
if i>length(A)
assignin('base','x',C); *<<<I just want C as output*
return
else
if isempty(P)==1
if isempty(X)==1
C{i,j}=R;
%R,P,X are changed
if isempty(P)==1
HELP(A,[],1:length(A),[],i+1,1,0,[],C);
else
HELP(A,R,P,X,i,j+1,n-1,D,C);
end
end
else
if n==0
some algo stuff
.
.
.
HELP(A,R,P,X,i,j,n+1,D,C);
else
algo stuff
.
.
.
.
.
.
HELP(A,R,P,X,i,j,n+1,D,C);
end
end
end
end
This is the framework of the code, I have debugged and I see it works, just some bad coding in the process.
Are you sure that termination condition is met? Apparently, the above code is logically correct and it should fall out of recursion once termination condition is met.
yes logically it's good, and yes the output falls out....but when I debugged it and ran it step by step it carries out further steps after it creates the variable 'x' in my workspace...
What do you mean by "further steps". The only statement after that is return. What exactly it is doing after
assignin('base','x',C);
haha exactly, after return it goes to HELP(....) below and starts editing my matrices, it's not an issue as I have my output but I would love to know what I'm doing wrong from a programming point of view ( if any).
From the code you gave, all calls to HELP function are followed by end. There are no statements after that. How can the function start editing matrices? Or are there any other statements which you haven't included in the given code?
One possible solution can be adding a return after every call to HELP. For example
if isempty(P)==1
HELP(A,[],1:length(A),[],i+1,1,0,[],C);
return;
else
HELP(A,R,P,X,i,j+1,n-1,D,C);
return;
end
Similar for other two calls to HELP.
thanks, added it in but didn't check in the debugger mode if it runs after as I get the desired output I need.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Operators and Elementary Operations 的更多信息

提问:

2018-4-25

评论:

2018-4-27

Community Treasure Hunt

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

Start Hunting!

Translated by