0

I have a function that I was wondering if it was possible to vectorize it and not have to use a for loop. The code is below.

a=1:2:8
for jj=1:length(a)
     b(jj)=rtfib(a(jj)); %fibbonacci function
end
b

output below:

a =

   1   3   5   7

>>>b =

    1    3    8   21

I was trying to do it this way

t = 0:.01:10;
y = sin(t);

but doing the code below doesn't work any suggestions? ps: I'm trying to keep the function rtfib because of it's speed and I need to use very large Fibonacci numbers. I'm using octave 3.8.1

 a=1:2:8
 b=rtfib(a)

Here's the rtfib code below as requested

function f = rtfib(n)

if (n == 0)
    f = 0;
elseif (n==1)
    f=1;
elseif (n == 2)
    f = 2;
else
    fOld = 2;
    fOlder = 1;
    for i = 3 : n
        f = fOld + fOlder;
        fOlder = fOld;
        fOld = f;
    end
end
end
Rick T
  • 3,349
  • 10
  • 54
  • 119
  • 5
    This probably depends on your `rtfib` function. Can you post the code? But also, you'll probably get as good a speedup (if you're in a reasonably current release of Matlab) by just preallocating `b`, i.e. before you loop type `b=zeros(size(a))`. Loops aren't that slow anymore – Dan Jan 26 '15 at 15:35
  • @Dan sure, I've added the rtfib code to the question I'm using octave 3.8.1 so it's not as fast as matlab yet – Rick T Jan 26 '15 at 15:47
  • Don't mind the vectorization, but instead of the double for-loop you could just use the single for loop of rtfib to save the whole vector! Linear instead of quadratic runtime! – knedlsepp Jan 26 '15 at 16:11
  • For all SO users visiting this post, the OP and subsequent people who provided an answer had a very nice discussion on creating the Fibonacci sequence efficiently and quickly. That code seen in the post above was from this post: http://stackoverflow.com/questions/26829209/create-faster-fibonacci-function-for-n-100-in-matlab-octave – rayryeng Jan 26 '15 at 21:01

1 Answers1

0

You can see that your function rtfib actually computes every Fibonacci number up to n. You can modify it so that is stores and returns all these number, so that you only have to call the function once with the maximum number you need:

function f = rtfib(n)

    f=zeros(1,n+1);
    if (n >= 0)
        f(1) = 0;
    end
    if (n>=1)
        f(2)=1;
    end
    if (n >= 2)
        f(3) = 2;
    end
    if n>2
        fOld = 2;
        fOlder = 1;
        for i = 3 : n
            f(i+1) = fOld + fOlder;
            fOlder = fOld;
            fOld = f(i+1);
        end
    end
end

(It will return fibonnaci(n) in f(n+1), if you don't need the 0 you could change it so that it returns fibonnaci(n) in f(n) if you prefer)

Then you only need to call

>>f=rtfib(max(a));
>>b=f(a+1)
b =

 1     3     8    21

If you don't want to store everything you could modify the function rtfib a little more, so that it takes the the array a as input, compute the Fibonacci numbers up to max(a) but only stores the one needed, and it would directly return b. Both solutions will slow down rtfib itself but it will be a lot faster than calculating the Fibonacci numbers from 0 each time.

yoh.lej
  • 1,104
  • 6
  • 14