Output Array, how to assign values from a function

I'm new to coding so just assume I know next to nothing. I've created my code for an assignment and have what I want, I don't don't understand how to manipulate it. Here's the code:
function [f] = fibanotchy
f=zeros(100,2,'uint64');
format long g
f(1)=1
f(2)=1
for k=3:100
f(k)=f(k-2)+ f(k-1);
colNames = {'F','n'};
sTable = array2table(f,'VariableNames',colNames)
It's the output from running the function I don't understand. It loops through the index k and displays every iteration. I thought ; suppressed that? Second, I've allocated the f array for the 100 values and a second row. I want to change those zeros to each iteration of the function (which is n, or nth number in the sequence). I can't do it. I've tried to define n=1:100 and put it in the array and can't. It seems simple but nothing I've tried works. I'm not sure how to access the second column of the matrix and get what I want in there. Or maybe I'm looking at it wrong. I've looked at Tables and maybe that's how to code it, but I'm not sure.

2 件のコメント

jonas
jonas 2018 年 10 月 28 日
I don't understand. The code seems incomplete.
furcifer
furcifer 2018 年 10 月 28 日
sorry jonas, i didn't put it in the code box. I've fixed it now so it's easier to read.

サインインしてコメントする。

 採用された回答

madhan ravi
madhan ravi 2018 年 10 月 28 日
編集済み: madhan ravi 2018 年 10 月 28 日

0 投票

fibanotchy
function [f] = fibanotchy
f=zeros(100,2,'uint64');
format long g
f(1)=1
f(2)=1
n=input('nth term ? ')
for k=3:n
f(k,1)=f(k-2)+ f(k-1);
f(k,2) = k;
end
f(1,2) =1;
f(2,2)=2;
end

3 件のコメント

furcifer
furcifer 2018 年 10 月 28 日
thanks again ravi. That's what I wanted to do. Can you explain the logic? I define my function f(k) then f(k,2)=k I can't see how to get to.
I believe this statement is something like "for matrix f, put k in column 1 and 2 in column 2, where k is equal to the 3 to 100 we defined it to be"
Then we set the first 2 values in the second column because that's how it was initially defined.
madhan ravi
madhan ravi 2018 年 10 月 28 日
Exactly you are getting the idea . And I defined f(1,2) as 1 and f(2,2) as 2 because the loop starts from 3 .
furcifer
furcifer 2018 年 10 月 28 日
編集済み: furcifer 2018 年 10 月 28 日
Ok. So if I wanted a 3rd column I'd allocate 3 instead of 2. Then define a variable and assign it f(var,k)=var
I would like to add a header. In my mind I have two options; set the first row to two strings 'Fibonacci' and 'N', then shift the rows all down, or put a title on the Column itself. The first seems like a bad habit to get into, but the second seems to be really complicated.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

タグ

質問済み:

2018 年 10 月 28 日

編集済み:

2018 年 10 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by