Find max value using parfor loop

6 vues (au cours des 30 derniers jours)
Lukasz Wieczorek
Lukasz Wieczorek le 29 Nov 2016
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by