Fibonacci sequence that fills an n*n matrix
15 visualizaciones (últimos 30 días)
Hey everybody. I'm trying to create an n x n matrix where the first two elements are input as well as n. It needs to fill the matrix row by row with each successive summation. I used a loop, but I'm welcome to other suggestions. Here's my code.
John D'Errico el 8 de Ag. de 2014
Editada: John D'Errico el 9 de Ag. de 2014
Adam - did you really intend to write it as:
Why would you use fib(n/n,k) for an assignment?
Surely you see that n/n is 1. So why not just assign it as
In fact, I have no idea why you are talking about an n*n matrix. There are n elements, not n*n elements.
You are creating a vector of length n. Note that when you index into the matrix, with f(k), you are treating it as a vector. So what do you intend when you talk about an nxn matrix?
And there is no need to initialize k before the start of the for loop. The for loop creates k as a variable.
As far as how to create a matrix of Fibonacci numbers, there are many ways to do so. The simplest way is to use filter, but that is not so obvious how it works if you are just learning to use a loop for the purpose.
n = input('n=\n');
fib = zeros(1,n);
fib(1) = input('First element=\n');
fib(2) = input('Second element=\n');
fib(k) = fib(k-1) + fib(k-2);
If you wish to create an array, then you need to explain what form it must take.
John Jairo Aguirre Sanchez el 31 de Dic. de 2018
%dimension de la matriz cuadrada
else if j==2