Several nested for-loops within parfor-loop variable indexing
Mostrar comentarios más antiguos
I am trying to run a Simulink model for a sensitivity analysis. Since I need to run the system with 5 different variables all with 6 different values, there are 7776 total trials I need to run. So I'm trying to use a parfor-loop to speed up the process.
I have a set of 4 nested for-loops within a parfor-loop. The problem is that MATLAB says, "The PARFOR loop cannot run due to the way variable 'savedVar' is used." How do I go about redefining or reindexing the variables such that parfor can run?
The loop structure looks something like this:
jn = 1:length(var2);
kn = 1:length(var3);
ln = 1:length(var4);
nn = 1:length(var5); % This is to avoid calling a function within the for-loop index variables
parfor i = 1:length(var1)
savedVar_temp_var2 = zeros(length(var2),length(var3),length(var4),length(var5))
savedVar_temp_var3 = zeros(length(var3),length(var4),length(var5))
savedVar_temp_var4 = zeros(length(var4),length(var5))
savedVar_temp_var5 = zeros(length(var5)) % Preallocating for my sliced variables
for j = 1:jn
for k = 1:kn
for l = 1:ln
for n = 1:nn
parsim('mysim.slx')
savedVar_temp_var5(n) = Vars; % Vars is the stand-in here for the
% various variables I want to save during the simulation
end
savedVar_temp_var4(l,:) = savedVar_temp_var5; % Assigning the variables
% entire rows/columns at a time (recommended in "Sliced
% Variables" documentation
end
savedVar_temp_var3(k,:,:) = savedVar_temp_var4;
end
savedVar_temp_var2(j,:,:,:) = savedVar_temp_var3;
end
savedVar(i,:,:,:,:) = savedVar_temp_var2; % Allocating all temp variables to actual array
saveVariablesFunc(savedVar) % MATLAB's "save" function does not work in parfor -- adding
% a function to do it works
end
% locally defined saving function
function saveVariablesFunc(savedVar)
save('myfile.mat','savedVar','-mat')
end
1 comentario
Paul
el 23 de Mzo. de 2023
I suspect there's probably a better way to accomplish the goal than using several loops. I suggest starting from this doc page for options and methods for running multiple simulations, if not read already.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!