Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Need help from more experienced programmers (MATLAB)

  1. #11
    Join Date
    Aug 2008
    Beans
    82

    Re: Need help from more experienced programmers (MATLAB)

    I think it would depend on what problem you are trying to solve. I do not think reversing a list with recursion would be any slower then useing a loop but it does have limitations such as the maximum recursion depth which you need to be mindful of if you need to change it for larger problems.

    Something like calculating the nth Fibonacci number using recursion would probably be much slower than useing a loop?

    I am not very experienced with recursion nor am I an "experienced" programmer so I make no claim that my thoughts above are accurate.

    XCan, your method of using a cell array is probably a more favorable method, but depending on that actual problem the OP was trying to solve I thought establishing a single base case and using recursion could possibly simplify the problem considerably as you mentioned.

    Regards Elbarto

  2. #12
    Join Date
    May 2009
    Beans
    13

    Re: Need help from more experienced programmers (MATLAB)

    Recursion in general is slower than doing things using a loop, or your own stack structure, as a recursive program is merely using the OS's stack to do its work. So if you care about speed (for example when a large number of recursive calls need to be made), avoid it.

    However, for some problems, it can be very intuitive to use recursion, making it an ideal choice for quick programming.

    But I do agree with elbarto, I think this program could be written recursively.

    --- start of function ---
    function [out1, out2] = f(x)
    function body
    ""
    ....

    f(out1)
    f(out2)

    --- end of function ---

  3. #13
    Join Date
    Aug 2008
    Beans
    82

    Re: Need help from more experienced programmers (MATLAB)

    I find the concept of recursion quite intriguing with some of the solutions it can yield rather easily. For example:

    http://en.wikipedia.org/wiki/Tower_of_Hanoi


    Regards Elbarto

  4. #14
    Join Date
    Aug 2007
    Beans
    876
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Need help from more experienced programmers (MATLAB)

    There is nothing wrong with recursion, I didn't mean to imply that. I just meant that it should not always be thought as the solution due to its performance issues. But for small problems, or problems where speed does not matter that much, when you have a choice to save tons of time using a recursive function, of course you should go for it.

  5. #15
    Join Date
    Aug 2005
    Location
    Fargo, ND, USA
    Beans
    1,499
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: Need help from more experienced programmers (MATLAB)

    In octave:
    Code:
    function ret = nest(func, arg, n)
        ret = arg;
        for i = 1:n
            ret = func(ret);
        end
    endfunction
    
    nest(@(x) [x,x], 0, 3) % returns the same as zeros(1,2^3)
    Help yourself: Search the community docs or try other resources.
    Quote Originally Posted by Henry Spencer
    Those who do not understand Unix are condemned to reinvent it, poorly.
    Let science use your computer when you aren't: Folding@Home.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •