Parallel for loop in loading files

1 次查看(过去 30 天)
Sultan
Sultan 2019-10-19
回答: Rik 2019-10-19
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.

回答(1 个)

Rik
Rik 2019-10-19

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.

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by