How to fix an infinite recursion?

8 views (last 30 days)
Pauline Li
Pauline Li on 1 Nov 2016
Answered: Lawal Abdmaleek on 13 Nov 2022
The assignment is to write a function that will search for a key in a vector and return the indices of all occurrences of the key but do not use the built in find function. It will receive two arguments- the vector and the key- and will return a vector of indices or the empty vector [] if the key is not found.
My problem is I get the "Out of memory.The likely cause is an infinite recursion within the program."
function idx = myfind(vec, key)
for vec=randi([1,10],1,10);
key=5;
idx = myfind(vec, key)
end
if key==0
idx=[];
end
This is my code, thank you in advance.

Answers (2)

Walter Roberson
Walter Roberson on 1 Nov 2016
In your code, after you set key=5, what is your expectation of what your line
idx = myfind(vec, key)
will do for you?
Why are you overwriting the vec and key that your routine was called with?
What I suspect is happening is that you are trying to invoke the functionality by pressing Run or clicking on the Run entry in the menus. You cannot usually do that with functions. Instead you have to go to the command line and type in the function name and appropriate arguments. For example, the command line you might type
myvec = randi([1,10],1,10)
mykey = 5;
idx = myfind(myvec, mykey)
  2 Comments
Pauline Li
Pauline Li on 1 Nov 2016
I wanted to find what elements in the vector "vec" is 5 without using find.
I do press RUN to test my function out, thank you for telling me that that is incorrect.
Walter Roberson
Walter Roberson on 1 Nov 2016
The strategy you have in the code is along the lines of:
Start running function myfind with some vector and key passed in. Overwrite the vector and key that were passed in. Call the same routine, myfind, passing in the newly-assigned vector and key. Which results in...
Start running function myfind, passing in the newly-assigned vector and key. Overwrite the vector and key with new values. Call the same routine, myfind, passing in the just-assigned vector and key. Which results in...
Start running function myfind, passing in the newly-assigned vector and key. Overwrite the vector and key with new values. Call the same routine, myfind, passing in the just-assigned vector and key. Which results in...
....
Eventually you run out of memory, without ever having actually done even one check to see whether the key is in the vector.
Recursion is useful for some tasks, but a key to recursion is that if you use it, each time you call it has to in some way be reducing the range of data to be executed over, so that eventually you reach the end of the range and can return a value upwards.
You really do not need recursion. You need a while() loop, or a for() loop, or logical indexing. The code is quite short and simple if you use logical indexing.

Sign in to comment.


Lawal Abdmaleek
Lawal Abdmaleek on 13 Nov 2022
function ret = AddTwoNumbers(a,b)
ret = a+b;
Num1 = 5;
Num2 = 15;
sumOfNumbers = AddTwoNumbers(Num1, Num2);
disp(sumOfNumbers);
end
AddTwoNumbers(5,15)
Out of memory. The likely cause is an infinite recursion within the program.
Error in AddTwoNumbers (line 6)
sumOfNumbers = AddTwoNumbers(Num1, Num2);
how do i solve this?

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!