MATLAB Answers

How to fix: Index exceeds matrix dimensions

조회 수: 1(최근 30일)
Chun Yong Koh
Chun Yong Koh 23 Aug 2019
댓글: Chun Yong Koh 24 Aug 2019
I am new to matlab and am trying to write a function which produces the Fibonacci sequence of input n. I am required to write it in a way so that the program returns all Fibonacci numbers which are less than 3000, including the index of the highest term below 3000. However, I kept getting the error ( Index exceeds matrix dimensions ) at where the if loops is located. May i know how I can change it to make it work?
function y=fibonacci(n)
for i=3:n
y(i) = y(i-1)+y(i-2);
i=i+1; %#ok<*FXSET>
if y(i) > 3000

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Torsten 23 Aug 2019
The for-loop increases i automatically. Thus remove the line "i=i+1".

  댓글 수: 1

Chun Yong Koh
Chun Yong Koh 24 Aug 2019
Really new to this but thanks for the help!

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by