RosettaCodeData/Task/Fibonacci-sequence/MATLAB/fibonacci-sequence-4.m

13 lines
344 B
Matlab

function number = fibonacci(n)
%construct the Tartaglia/Pascal Triangle
pt=tril(ones(n));
for r = 3 : n
% Every element is the addition of the two elements
% on top of it. That means the previous row.
for c = 2 : r-1
pt(r, c) = pt(r-1, c-1) + pt(r-1, c);
end
end
number=trace(rot90(pt));
end