Parallel nested for-Loop with a compounding variable
이전 댓글 표시
Hi I'm trying to execute a code that contains three for-loops with the innermost loop containing a variable whose value gets compounded at every iteration.
Here is an example:
A = [];
for i = 1:10
for j = 1:10
B = f(i,j);
for k = 1:10
A = A + g(B);
end
end
end
The nested for-loop itself cannot be worked around unfortunately but this is taking too long computation-wise I am trying to incorporate "parfor", the matlab does not allow variable "A". Is there a workaround to this?
Thanks!
채택된 답변
추가 답변 (2개)
Walter Roberson
2015년 10월 5일
0 개 추천
If you initialize A as 0 then you can do it with parfor, as A would then be recognized as a "reduction variable". The calculation might internally be done with subtotals that are then totaled, so if your calculation depends critically on the order in which the totals are done then parfor is not appropriate. For example, ((2 + (-2)) + eps(1)) would have a different result than (2 + ((-2) + eps(1)) so if you have carefully programmed your loop to avoid those kinds of round-off problems then you would want to be very careful how you programmed your parfor.
카테고리
도움말 센터 및 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!