function fib = myfibonacci(n) % myfibonacci calculate first n Fibonacci numbers fib = ones(n, 1); % preallocate the vector; % automatically initialize fib(1) (and fib(2) if n>=2) % the loop below is not executed if n<3 for i = 3:n % starting the iterations from i=3 since fib(1) / fib(2) are already initialized fib(i) = fib(i-1) + fib(i-2); end end