Help correcting the sequence function

Hello , i was wondering if anyone could help me out a little bit , so am writing a function ,which calculates sequence in a matrix diaognaly , but am little bit stuck ,its not givng the numbers that i want
n=input('sequence_matrix_')
fibb=[1,3:n];
for i=3:n
fibb(i)=fibb(i-1)*3+1;
end
(fibb)
I want it to display
1,3,10,33,109,360

回答(1 个)

% if you're asking the user for a number, use a clear description
% "sequence matrix" doesn't tell anyone what the number means
n = 10; % number of elements in sequence
fibb = zeros(1,n);
fibb(1:2) = [1 3]; % only the first two numbers are used
for i = 3:n
fibb(i) = fibb(i-1)*3 + fibb(i-2); % only +1 for first iteration
end
fibb
fibb = 1×10
1 3 10 33 109 360 1189 3927 12970 42837

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

提问:

2021-7-19

编辑:

DGM
2021-7-19

Community Treasure Hunt

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

Start Hunting!

Translated by