How do I implement Parfor loop with nested for loops?
古いコメントを表示
Hi
I'm doing a Brute Force search by varying 5 parameters, running a Simulink model and then calculating a cost function.
I have a Matlab script which contains the following:
simTime = 2; %2 seconds simulation time
N = 5; % number of time step
brakeRange = 0:1/(10-1):1; % Vector of possible brake value (percent)
timeVec = 0:simTime/(N-1):simTime;
leng = length(brakeRange);
data = zeros(leng^N,N+1);
a =1;
tic
for i = 1: leng
s = sprintf('\n Percentage complete = %s % ',num2str((a/leng^N)*100));
disp(s);
for j = 1: leng
for k = 1: leng
for l = 1: leng
for m = 1: leng
% Index input vector
inVec = [brakeRange(i) brakeRange(j) brakeRange(k) brakeRange(l) brakeRange(m)];
% run simulation
simOut = sim(mdl,'SimulationMode','Accelerator','ReturnWorkspaceOutputs', 'on');
% Get cost from simulation
tempJ = simOut.get('cost');
%packing data struct
data(a,:) = horzcat(tempJ(end), inVec);
% increment counter
a = a +1;
end
end
end
end
end
Unfortunately, the code is very slow, so I decided to purchase the Parallel Computing Toolbox. I tried to use Parfor, but I get the error message "the PARFOR loop cannot runt due to the way data is used." I get the same message for the variable 'a'.
I think the problem has to do with sliced variables (?) but I'm not sure as I'm new to Parallel Computing. How does one implement parfor loops with nested for loops?
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!