简体   繁体   English

使用 matlab 循环的斐波那契矩阵

[英]Fibonacci matrix using loops of matlab

I want to create an MxN matrix of the Fibonacci series.我想创建一个斐波那契数列的 MxN 矩阵。

My Matlab function should take two integers that are M and N and return a two-dimensional array of Fibonacci series like我的 Matlab function 应该采用两个整数 M 和 N 并返回斐波那契数列的二维数组,例如

A =一个=

1 1 2 3 5 8 13 21 34 1 1 2 3 5 8 13 21 34

1 2 3 5 8 13 21 34 55 1 2 3 5 8 13 21 34 55

2 3 5 8 13 21 34 55 89 2 3 5 8 13 21 34 55 89

3 5 8 13 21 34 55 89 144 3 5 8 13 21 34 55 89 144

5 8 13 21 34 55 89 144 233 5 8 13 21 34 55 89 144 233

8 13 21 34 55 89 144 233 377 8 13 21 34 55 89 144 233 377

I just could create 1 row of the matrix我只能创建 1 行矩阵

function A = double_fibonacci(M,N)
A = ones(M,N);
for ii = 1:M
    for jj = 3:N
        A(ii,jj) = A(ii,jj-1) + A(ii,jj-2);
    end
end
end

Thanks in advance.提前致谢。

If you want to build your matrix with a double loop, then after you finish the first line, you need to prepare the next line so the fibonacci calculations can be done with the same method than for the first line.如果要使用双循环构建矩阵,则在完成第一行后,您需要准备下一行,以便可以使用与第一行相同的方法完成斐波那契计算。 This "preparation" involve copying the 2nd and 3rd element of the current line into the 1st and 2nd position of the next line.这种“准备”包括将当前行的第二个和第三个元素复制到下一行的第一个和第二个 position 中。

This would look like this:这看起来像这样:

function A = double_fibonacci_loop(M,N)
A = ones(M,N);
for ii = 1:M
    % build the line normally
    for jj = 3:N
        A(ii,jj) = A(ii,jj-1) + A(ii,jj-2);
    end
    % if we're not on the last line, we copy the 2nd and 3rd element of the
    % current line into the 1st and 2nd element of the next line, so the
    % fibonacci calculation can proceed as in the block above
    if ii<M
        A(ii+1,1:2) = A(ii,2:3) ;
    end
end

However, if you don't specifically need a double loop, I would propose another way to build that matrix.但是,如果您不是特别需要双循环,我会提出另一种构建该矩阵的方法。 Just compute only once a Fibonacci suite with all the elements required, then copy the relevant elements in each line of your final matrix.只需计算一次包含所有所需元素的斐波那契套件,然后在最终矩阵的每一行中复制相关元素。

This would look like:这看起来像:

function A = double_fibonacci(M,N)

%% Construct a single fibonacci suite with the required number of elements
nElements = M+N-1 ;
basefib = ones(1,nElements) ;
for k=3:nElements
    basefib(k) = basefib(k-1) + basefib(k-2) ;
end

% After that block, basefib =
% [ 1  1  2  3  5  8  13  21  34  55  89  144  233  377 ]

%% Now dispatch the relevant elements in each line of your matrix
A = ones(M,N);
for k=1:M
    A(k,:) = basefib(k:k+N-1) ;
end

And just to make sure, both function output the same result:只是为了确保 function output 的结果相同:

>> A = double_fibonacci(6,9)
A =
     1     1     2     3     5     8    13    21    34
     1     2     3     5     8    13    21    34    55
     2     3     5     8    13    21    34    55    89
     3     5     8    13    21    34    55    89   144
     5     8    13    21    34    55    89   144   233
     8    13    21    34    55    89   144   233   377

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM