Modify Fibonacci Function to Return nth Number - Matlab Homework

  • Thread starter Thread starter Joon
  • Start date Start date
  • Tags Tags
    Matlab Sequence
AI Thread Summary
The discussion focuses on modifying a Fibonacci function in MATLAB to return only the nth number instead of the entire sequence. Participants express confusion about how to implement this change, noting that only the previous two Fibonacci numbers are needed to compute the next one, which suggests that storing all numbers in an array is unnecessary. A suggestion is made to use scalar variables and a loop to achieve the desired result efficiently. Additionally, there is a mention of using indexing techniques, such as accessing specific elements in matrices or vectors. The conversation emphasizes the importance of understanding how to manipulate sequences in MATLAB to streamline the function.
Joon
Messages
85
Reaction score
2

Homework Statement


Modify the function so that instead of returning all n numbers, it only returns the nth number.

Homework Equations

The Attempt at a Solution


I'm not sure how to return only the nth number of the fibonacci sequence. Please help.
 

Attachments

  • fibonacci problem.png
    fibonacci problem.png
    6.4 KB · Views: 424
Physics news on Phys.org
If I gave you a sequence of any random set of numbers, could you return only the Nth one?
 
I know how to return a number in a nth row / nth column matrices/vectors but I'm confused where to start.
 
For example, I know that c = A(2,2:2:end); sets c equal to columns {2,4,6,8,10} of row 2 of A
and that c=A(2,2) sets c equal to the number that is in row 2 column 2 of A.
 
If you only are required to return the nth number I don't see why you'd bother to store them all in an array; you only need the previous two to generate the next one. You should be able to do this with a small set of scalar variables and a loop.
 
Joon said:
For example, I know that c = A(2,2:2:end); sets c equal to columns {2,4,6,8,10} of row 2 of A
and that c=A(2,2) sets c equal to the number that is in row 2 column 2 of A.

So you're familar with END. Are you aware that f(end) is the last element of vector f?
 

Similar threads

Replies
4
Views
5K
Replies
3
Views
6K
Replies
1
Views
2K
Replies
2
Views
5K
Replies
2
Views
2K
Replies
11
Views
2K
Replies
6
Views
4K
Back
Top