for loop - MATLAB: parfor error -
i have following matlab code want run using parfor:
max = -1; = 1:10 j = (i+1):10 x = my_function(i, j); if (x > max) max = x; end end end disp(max)
i want change first parfor. read couple of tutorials , documentation, don't know how have same result max, using parfor.
i know there problem usage of in for j = (i+1):10
.
i appreciate suggestion.
you can not use parfor
dependent iterations, i.e. in case max
dependent (shared) variable between loop iterations:
you cannot use parfor-loop when iteration in loop depends on results of other iterations. each iteration must independent of others.
this reflected in displayed warning message:
warning: temporary variable
max
cleared @ beginning of each iteration of parfor loop. value assigned before loop lost. ifmax
used before assigned in parfor loop, runtime error occur. see parallel loops in matlab, "temporary variables".
matlab implements one exception rule, i.e. reduction variables:
the exception rule accumulate values in loop using reduction variables.
so, can rewrite code use reduction variables:
maxx = -1; = 1:10 j = (i+1):10 maxx = max(maxx, my_function(i, j)); end end disp(maxx)
Comments
Post a Comment