Parallel nested for-Loop with a compounding variable
Ältere Kommentare anzeigen
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!
Akzeptierte Antwort
Weitere Antworten (2)
Walter Roberson
am 5 Okt. 2015
0 Stimmen
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.
Jeongseop Lee
am 6 Okt. 2015
0 Stimmen
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!