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 Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by