MATLAB Answers

How to fix: Index exceeds matrix dimensions

1 ビュー (過去 30 日間)
Chun Yong Koh
Chun Yong Koh 2019 年 8 月 23 日
コメント済み: Chun Yong Koh 2019 年 8 月 24 日
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 件のコメント

サインイン to comment.

回答 (1 件)

Torsten 2019 年 8 月 23 日
The for-loop increases i automatically. Thus remove the line "i=i+1".

  1 件のコメント

Chun Yong Koh
Chun Yong Koh 2019 年 8 月 24 日
Really new to this but thanks for the help!

サインイン to comment.

サインイン してこの質問に回答します。

Translated by