Recursion Limit Reached Question
10 次查看(过去 30 天)
显示 更早的评论
I am trying to write the recursive code for the Longest Common Subsequence problem and I run into the errror, "Maximum recursion limit reached." The code is:
function result=bin2(n,k)
if (isempty(k) || isempty(n))
result = 0;
elseif n == k
result = bin2(n-1, k-1) +1;
else
result = max((bin2(n-1, k)), (bin2(n, k-1)));
end
with the inputs n = [1,9,5,2,8,6,3,7,4,10] and k = [3,7,10,4,9,5,8,2,6,1] this code is adapted straight from the pseudocode in the text and should give the length of the LCS of the inputs. I don't understand what I've done wrong. Please advise.
0 个评论
采纳的回答
Matt J
2013-6-27
编辑:Matt J
2013-6-27
This
if (isempty(k) || isempty(n))
result = 0;
doesn't seem like it will ever stop the recursion. All you ever do to modify "n" and "k" within the code is to subtract 1 from them. This operation will never make them empty. Perhaps you meant to do something else to them besides subtracting 1.
0 个评论
更多回答(1 个)
Matt J
2013-6-27
编辑:Matt J
2013-6-27
MATLAB has a default limit on the number of recursive calls you can make for the protection of people who do recursion by accident. The error message tells you how to increase the limit from the default, if you really need to do more recursions, but you should probably build some sensible limit into your own code to prevent crashes.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!