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

Ameer Hamza
Ameer Hamza 2018년 4월 25일
Can you show us your friends function code, along with data you are using to call that function?
Rdmn Raja
Rdmn Raja 2018년 4월 25일
편집: Rdmn Raja 2018년 4월 25일
[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.

Ameer Hamza
Ameer Hamza 2018년 4월 25일

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

Rdmn Raja
Rdmn Raja 2018년 4월 25일
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.

Rdmn Raja
Rdmn Raja 2018년 4월 25일
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.

Rdmn Raja
Rdmn Raja 2018년 4월 25일
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.
Ameer Hamza
Ameer Hamza 2018년 4월 26일
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.
Ameer Hamza
Ameer Hamza 2018년 4월 26일
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.
Rdmn Raja
Rdmn Raja 2018년 4월 26일
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);
Rdmn Raja
Rdmn Raja 2018년 4월 26일
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.
Rdmn Raja
Rdmn Raja 2018년 4월 27일
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