Return function in a recursive function code
1 次查看(过去 30 天)
显示 更早的评论
Rdmn Raja
2018-4-25
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
2018-4-25
Can you show us your friends function code, along with data you are using to call that function?
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
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.
Walter Roberson
2018-4-25
编辑:Walter Roberson
2018-4-25
The line
Function(A,P,K)
does not change C, so there is no point running the call.
Rdmn Raja
2018-4-25
true, C is only determined right at the end, can I display the output as a matrix using disp(..)
Ameer Hamza
2018-4-25
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
2018-4-26
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
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.
Rdmn Raja
2018-4-26
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
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
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...
Ameer Hamza
2018-4-26
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
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).
Ameer Hamza
2018-4-26
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
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
