Parallel for loop in loading files

1 vue (au cours des 30 derniers jours)
Sultan
Sultan le 19 Oct 2019
Réponse apportée : Rik le 19 Oct 2019
I would like to make the following code in parallel. Could you please help me?
clear all; clc;
for kk= 1:30014
m = 0; n = 0;
for j = 1:5
load(['File' num2str(kk) '_' num2str(j) '.mat']);
iteration(j) = iter;
FScore(j) = score;
NDistance(j)= distance;
allF(j,:) = F;
if FScore(j) < 0
m = m+1;
negF(m,:) = F;
negFScore(m) = score;
negDistance(m) = distance;
else
n =n+1;
posF(n,:) = F;
posFScore(n) = score;
posDistance(n) = distance;
end
end
[minFScore,minFScoreIndex] = min(FScore);
[minDistance, minDistanceIndex] = min(NDistance);
if minFScore < 0
[minNegFScore,minNegFScoreIndex] = min(negFScore);
[minNegDistance, minNegDistanceIndex] = min(negDistance);
allFVoilatingH = negH(minNegFScoreIndex,:);
allFVoilatingAndMinDistH = negH(minNegDistanceIndex,:);
filename = ['nv_' num2str(kk) '.mat'];
save(filename,'negDistance','allFVoilatingH', 'allFVoilatingAndMinDistH', 'minDistance','minNegFScore','minNegFScoreIndex','minNegDistance','minNegDistanceIndex','minDistanceIndex','allF','normDistance','negFScore','negH', 'minFScore','minFScoreIndex', 'FScore', 'iteration');
else
filename = ['pv_' num2str(kk) '.mat'];
save(filename, 'allF','NDistance','minFScore','minFScoreIndex', 'FScore', 'iteration');
end
clear all; clc;
end
Thanks in advance.

Réponses (1)

Rik
Rik le 19 Oct 2019

Removing clear all from your code and loading to a struct (instead of poofing variables into existence) should allow you to replace the outer for loop by a parfor. Don't forget to preallocate your variables in the loop, instead of implicitly setting them to empty arrays with clear.

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by