Parallel for loop in loading files

5 visualizaciones (últimos 30 días)
Sultan
Sultan el 19 de Oct. de 2019
Respondida: Rik el 19 de Oct. de 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.

Respuestas (1)

Rik
Rik el 19 de Oct. de 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.

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by