Picking only 2 nested for loops among several.

조회 수: 5 (최근 30일)
Arnaud
Arnaud 2025년 5월 13일
편집: Matt J 2025년 5월 14일
I have a very complex code structure with nested for loops.
Some days i loop over 2 given variables, some days two other ones, some days only one.
I want to limit the number of lines i have to edit every day when i want to run a different version of the code.
params = initialization(params_file_path);
for ii_outer_variable_1 = 1:length(params.outer_variable_1_values)
outer_variable_1 = params.outer_variable_1_values(ii_outer_variable_1);
...
for ii_outer_variable_5 = 1:length(params.outer_variable_5_values)
outer_variable_5 = params.outer_variable_5_values(ii_outer_variable_5);
array_1 = get_file_1(outer_variable_1, outer_variable_2, outer_variable_5);
array_2 = get_file_1(outer_variable_3, outer_variable_4, outer_variable_5);
[array_1, array2] = manipulate_and_combine_those_files(array_1, array2, params);
plot_some_visuals_1(array_1, array2, params);
for ii_middle_variable_1 = 1:length(params.middle_variable_1_values)
middle_variable_1 = params.middle_variable_1_values(ii_middle_variable_1);
[array_1, array2] = reshape_my_arrays(array_1, array2, middle_variable_1, params);
plot_some_visuals_2(array_1, array2, params);
for ii_inner_variable_1 = 1:length(params.inner_variable_1_values)
inner_variable_1 = params.inner_variable_1_values(ii_inner_variable_1);
...
for ii_inner_variable_4 = 1:length(params.inner_variable_4_values)
inner_variable_4 = params.inner_variable_4_values(ii_inner_variable_4);
result = iterative_script(array_1,array_2,params,inner_variable_1,inner_variable_2,inner_variable_3,inner_variable_4); % This iterative_script has hundreds of iterations of large calculations and thus runs for hours. The main script runs for 10h to a few days.
% I don't fill the results cell array yet otherwise Matlab would run out of memory.
savename = sprintf("%s%d%s%d%s.mat",params.path,variable_of_the_day_1,name_of_variable_of_the_day_1,variable_of_the_day_2,name_of_variable_of_the_day_2);
save(savename,"result");
end
end
end
end
end
end
end
end
end
end
clear all -except params
results = cell(length(params.variable_of_the_day_1_values),length(params.variable_of_the_day_2_values));
for ii_variable_of_the_day_1 = 1:length(params.variable_of_the_day_1_values)
variable_of_the_day_1 = variable_of_the_day_1_values(ii_variable_of_the_day_1);
for ii_variable_of_the_day_2 = 1:length(params.variable_of_the_day_2_values)
variable_of_the_day_2 = variable_of_the_day_2_values(ii_variable_of_the_day_2);
results{ii_variable_of_the_day_1,ii_variable_of_the_day_2} = load(sprintf("%s%d%s%d%s.mat",params.path,variable_of_the_day_1,name_of_variable_of_the_day_1,variable_of_the_day_2,name_of_variable_of_the_day_2)).result;
end
end
my_display(results);
So, some days i will for example have both params.outer_variable_1_values and params.middle_variable_1_values be a 1*10 array each, and all other params.xx_values arrays be only 1*1 scalar arrays to avoid having incredibly complex file names (and so many result files that i can't even plot them vs. each other without flooding the screen).
I don't have hopes of reducing the number of nested for loops, but at least i don't want to everyday have to rewrite every input of every sprintf() call that i run for fig titles and filesave names, and rewrite every field of the two for loops at the end that display the results.
Can i somehow use "@" (handles to variables) tricks for that ?

답변 (1개)

Matt J
Matt J 2025년 5월 13일
편집: Matt J 2025년 5월 13일
Get rid of all the nested loops. Use the combinations command to create a table of all combinations of your variables. Then, you can just use a single for-loop across the rows of the table to get each combination.
T=combinations(1:3,1:2,1:4)
T = 24x3 table
Var1 Var2 Var3 ____ ____ ____ 1 1 1 1 1 2 1 1 3 1 1 4 1 2 1 1 2 2 1 2 3 1 2 4 2 1 1 2 1 2 2 1 3 2 1 4 2 2 1 2 2 2 2 2 3 2 2 4
[m,n]=size(T);
s=table2struct(T);
c=table2cell(T);
nameTemplate="name"+repmat('_%d',1,n)
nameTemplate = "name_%d_%d_%d"
for i=1:m
fname=sprintf(nameTemplate,c{i,:});
si=s(i);
save(fname,'-struct','si');
end
dir '*.mat'
name_1_1_1.mat name_1_1_4.mat name_1_2_3.mat name_2_1_2.mat name_2_2_1.mat name_2_2_4.mat name_3_1_3.mat name_3_2_2.mat name_1_1_2.mat name_1_2_1.mat name_1_2_4.mat name_2_1_3.mat name_2_2_2.mat name_3_1_1.mat name_3_1_4.mat name_3_2_3.mat name_1_1_3.mat name_1_2_2.mat name_2_1_1.mat name_2_1_4.mat name_2_2_3.mat name_3_1_2.mat name_3_2_1.mat name_3_2_4.mat
  댓글 수: 11
Arnaud
Arnaud 2025년 5월 14일
"use conditional logic for that, too"
Yes, i think that would work, thanks i'll try it !
Arnaud
Arnaud 2025년 5월 14일
"Stephen already explained that you can make the file reading and whatever else conditional"
Yes, i'm sorry i wrote that before reading the "and you detect when there is a change in the corresponding output element" part.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by