피보나치 숫자를 계산하기 위해 MatLab 함수를 작성하려고합니다. 아래에 나와있는 내용이 있지만 F(0)
에 대한 오류가 발생합니다.처음 두 변수 만 지정하는 MatLab
??? Attempted to access F(0); index must be a positive integer or logical.
Error in ==> fibonacci at 11
F(0) = 0;
어떻게 배열의 첫 번째 두 값이 0과 1 것을 MATLAB을 알 수 있습니까?
function F = fibonacci(n)
%A fibonacci sequence is where the next term in the series is given by the
%sum of the pervious two terms
%Only valid if n is greater than or equal to 2
if n >= 2 ;
%Make an array with n terms
F = zeros (1,n);
%run a for loop from 2 to n
for i = 2:n;
F(0) = 0;
F(1) = 1;
F(i) = F(i-1) + F(i-2)
end
end
end
인덱스를 제로로 할 수 없으며, 인덱스 + 1을 사용하여 배열에 액세스 할 수 있습니다. – Alex