Picking only 2 nested for loops among several.
조회 수: 5 (최근 30일)
이전 댓글 표시
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 ?
댓글 수: 0
답변 (1개)
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)
[m,n]=size(T);
s=table2struct(T);
c=table2cell(T);
nameTemplate="name"+repmat('_%d',1,n)
for i=1:m
fname=sprintf(nameTemplate,c{i,:});
si=s(i);
save(fname,'-struct','si');
end
dir '*.mat'
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!