vector uses info from itself to grow without for cycle
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2020-3-27
Which of the two?
Recursive functions with no explicit loop are easy for this.
A closed form formula might be difficult.
采纳的回答
Walter Roberson
2020-3-28
编辑:Walter Roberson
2020-3-28
function A = calculate_A(n)
if n == 1
A = 1/2;
else
A_before = calculate_A(n-1);
A = [A_before; (1-n*sum(A_before))/(1+n)];
end
end
Note: this will fail at roughly 75000, due to the recursion using up memory.
0 个评论
更多回答(1 个)
Ameer Hamza
2020-3-26
编辑:Ameer Hamza
2020-3-26
For the original code in your question. Following is the simplified form.
k = 1:20;
A = 6.^(k-1);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!