Cher
Cher

Reputation: 2947

How to build a vector one element by one element?

In Matlab, I need to build a vector with the first 20 Fibonacci numbers. I have a function which returns them. How can I add every element my function returns one by one into my vector?

Upvotes: 0

Views: 41

Answers (1)

P0W
P0W

Reputation: 47844

Assuming you have some fibonacci function

Then can do something like this :-

N = 20; %// Total count

vect= zeros(1,N);
vect(1) = 1;
for k = 2:N
    vect(k) = fibonacci(k-1); % Your fibonacci function
end

Upvotes: 1

Related Questions