Find max value using parfor loop

6 views (last 30 days)
I have very complex, time-consuming computation function depends on three parameters. I decided to use parfor loop and I have the following situation
best_result = methodcall(init1, init2, init3);
for x1=1:init1
for x2=1:init2
parfor x3=1:init3
a = methodcall(x1,x2,x3);
if (a > best_result)
best_result = a;
end
end
end
end
And of course -
The temporary variable 'best_result' uses a value set outside the PARFOR.
I cannot define an 3d array (init1, init2, init3) because there are very big numbers. What I have to do, to solve it?

Accepted Answer

Walter Roberson
Walter Roberson on 29 Nov 2016
best_result = methodcall(init1, init2, init3);
for x1=1:init1
for x2=1:init2
a = zeros(init3, 1);
parfor x3=1:init3
a(x3) = methodcall(x1,x2,x3);
end
best_result = max(best_result, max(a));
end
end
Note: your heading talks about finding the max value, but your proposed code would find the min value. I have coded here for max value; change the two max() to min() if you want min.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!