Recursive computation without loop
이전 댓글 표시
Hi! Can I write the following code
for j = 2 : N-1
alpha(j+1,:) = A(j,:).*alpha(j,:) + B(j,:);
end
in a form like this:
J = 2:N-1;
alpha(J+1,:) = A(J,:).*alpha(J,:) + B(J,:);
I tried to use this form but the alphas are incorrectly calculated.
댓글 수: 6
"Recursive computation without loop"
Recursive calculations do not require loops: loops are iterative.
Why do you want to get rid of the loop: what is the problem with it?
avenior
2018년 4월 1일
"To speed up these calculations. In vector form they are much faster executed."
How do you know this? It looks to me like premature optimization, which is a classic programming anti-pattern:
Walter Roberson
2018년 4월 1일
You got faster code that calculated the wrong thing.
With the loop the value of B(1,:) affects alpha(2,:), and that has an effect that changes all later output. With the vectorized version you do not get the feedback of earlier B values affecting all later values.
avenior
2018년 4월 1일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!