vector uses info from itself to grow without for cycle

조회 수: 1 (최근 30일)
gabriele fadanelli
gabriele fadanelli 2020년 3월 26일
편집: Walter Roberson 2020년 3월 28일
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
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.
gabriele fadanelli
gabriele fadanelli 2020년 3월 28일
편집: gabriele fadanelli 2020년 3월 28일
even recursive function would be nice thank you. I just need an example.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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.

추가 답변 (1개)

Ameer Hamza
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);
  댓글 수: 1
gabriele fadanelli
gabriele fadanelli 2020년 3월 26일
you are right, but I am actually looking for above problem solution, sorry.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by