vector uses info from itself to grow without for cycle
Show older comments
I need to solve this problem without a for loop:
B= (1:20)
A = [];
A(1) = 1/(1+B(1));
for k = 2:length(B)
A(k,1) = (1-B(k)*sum(A))/(1+B(k));
end
i.e. I need to know if it is possible to get information from prebvious calculation to create a vector, but without a for loop. Thanks.
13 Comments
darova
on 26 Mar 2020
You code looks weird
gabriele fadanelli
on 26 Mar 2020
Edited: gabriele fadanelli
on 26 Mar 2020
gabriele fadanelli
on 26 Mar 2020
Guillaume
on 26 Mar 2020
What is the final A that your code should be producing?
gabriele fadanelli
on 26 Mar 2020
Edited: gabriele fadanelli
on 26 Mar 2020
Guillaume
on 26 Mar 2020
Rather than making us guess what you mean, I'll repeat, what is the desired final (20x1) A for your code above? At least give us the first few elements of the sequence. Alternatively, please write the recursion mathematically.
gabriele fadanelli
on 26 Mar 2020
Walter Roberson
on 26 Mar 2020
Your code talks about length(S) but it only uses S(1) and no other S element?
gabriele fadanelli
on 26 Mar 2020
Walter Roberson
on 27 Mar 2020
Are you looking for a closed form formula that can give A(K) without calculating A(K-1) ?
Are you looking for a recusive function that can calculate A(K) by calling itself, without an explicit loop?
gabriele fadanelli
on 27 Mar 2020
Walter Roberson
on 27 Mar 2020
Which of the two?
Recursive functions with no explicit loop are easy for this.
A closed form formula might be difficult.
gabriele fadanelli
on 28 Mar 2020
Edited: gabriele fadanelli
on 28 Mar 2020
Accepted Answer
More Answers (1)
Ameer Hamza
on 26 Mar 2020
Edited: Ameer Hamza
on 26 Mar 2020
For the original code in your question. Following is the simplified form.
k = 1:20;
A = 6.^(k-1);
1 Comment
gabriele fadanelli
on 26 Mar 2020
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!