-2

I have to let Matlab run a huge equation, and I want to have infinite recursive equation. How can I set it? I can set a number, but how can I say never stop? If it stops, it's because compute can't handle it.

General command is this:

set(0,'RecursionLimit',999999)
Cher
  • 2,789
  • 10
  • 37
  • 64
  • 1
    have you try to change it to its iterative form? – Copperfield Sep 24 '16 at 15:01
  • just spent lots of time doing it... so I would hope I don't have to... – Cher Sep 24 '16 at 15:04
  • 2
    Each recursive call takes up some memory to store parameters, usually on a stack. So it can never be _infinite_. Not sure what you really mean – Luis Mendo Sep 24 '16 at 15:04
  • if you can change your function to tail recursion, then a iterative form is easy to make – Copperfield Sep 24 '16 at 15:06
  • @LuisMendo I want it to use all memory, so if it crashes, it's because I lack memory. All my function does is from dimension nxm, create every possibility of 0 1. For example, a 1X3 would create [ 0 0 0 ] [ 0 0 1 ] [ 0 1 0 ] [ 0 1 1 ] ... etc – Cher Sep 24 '16 at 15:09
  • 1
    @Cher That can be done iteratively (not recursiveley) very easily. Or even in vectorized form (no loops): see for example the result of `n = 5; y = dec2bin(0:2^n-1)-'0';` – Luis Mendo Sep 24 '16 at 15:11
  • @LuisMendo thx a lot! have I known that it would have solved so much time! could I askthe same for a mX n matrix? – Cher Sep 24 '16 at 15:18
  • @Cher What would the input and desired output be? – Luis Mendo Sep 24 '16 at 15:21
  • I would say m line, n column, and I would need to do something on every possible matrix of mXn. Every possible matrix being all possible combination of 0 and 1. example for a 2X2 (I will write on one line the 2 rows): [ 0 0 0 0 ] [ 0 0 0 1 ] [ 0 0 1 0 ] [ 0 0 1 1 ] [ 0 1 0 0 ] ... – Cher Sep 24 '16 at 15:22
  • You can use `y = dec2bin(0:2^n-1)-'0'` and reshape `y` as needed. Maybe post a new question with exact input and desired output. Would the output be a 3D array? – Luis Mendo Sep 24 '16 at 15:24
  • here : http://stackoverflow.com/questions/39678003/how-to-have-every-0-1-possible-combination-in-a-nxm-matrix – Cher Sep 24 '16 at 15:28

1 Answers1

2

The only way to use infinite recursion is to use tail recursion, but Matlab doesn't support tail recursion.

You can increase your recursion limit compatibly with available memory.

Otherwise you must convert the code to avoid recursion.


https://www.quora.com/What-is-infinite-recursion

Recursion theoretically can be infinite, but if we intend to use infinite recursion in a real program, it means that the program never ends.

A recursive function consumes memory (stack memory): https://mitpress.mit.edu/sicp/full-text/sicp/book/node110.html

Since the memory is limited, you can never use a true infinite recursion.

Furthermore, the stack size must defined in advance because the stack needs to be stored in continuous memory locations: why is stack memory size so limited?

But in computer programming there is a trick called "Tail Recursion", with Tail Recursion, the compiler or interpreter translated the code into an optimized non recursive code: How exactly does tail recursion work?

http://c2.com/cgi/wiki?TailRecursion

Personally I don't know any other solution in computer programming to write a recursive function without consumes stack.

MatLab doens't support Tail Recursion optimization:

https://it.mathworks.com/matlabcentral/answers/16309-tail-recursive-function-and-wrapper-function

Sorry for bad english.

Stefano Balzarotti
  • 1,760
  • 1
  • 18
  • 35
  • *"The only way to use infinite recursion is to use tail recursion..."* I don't believe this is true. Do you have a proof or a reference? *"...but Matlab doesn't support it."* What is *"it"*, tail recursion or infinite recursion? – beaker Sep 25 '16 at 16:08
  • I edited the answer, because I don't have enough space in comment, I hope I was clear. – Stefano Balzarotti Sep 25 '16 at 16:47