How to speed up processing and reduce memory with nested for loops
2 views (last 30 days)
Show older comments
This program calculates optical transmittance in seawater for different combinations of optical filters and then finds the best statistical match to a target curve. It appears to work well (experimentally unverified) with 16 filters and lower constraints but I needed to see the closest (theoretical) match that could be obtained with all the 48 filters that I have data for.
It basically does this calculation: by using 48 nested for loops to increment the a, b, n variables. This is workspace I start with I start with:
T_Calc = double.empty(0,1E6);
Index = single.empty(0,1E6);
Total_FilterConstraint = 8; %This is the total number of filters used in the solution, not the number of filter's data sampled
Ind_FilterConstraint = 4;
for A= 0:1:Ind_FilterConstraint
for B= 0:1:Ind_FilterConstraint
Total = A+ B;
if Total > Total_FilterConstraint
break;
end
for C= 0:1:Ind_FilterConstraint
Total = A+ B+ C;
if Total > Total_FilterConstraint
break;
end
.... Shortened for forum (there are 48 of these)
for AV = 0:1:Ind_FilterConstraint
Total = A+ B+ C+ D+ E+ F+ G+ H+ I+ J+ K+ L+ M+ N+ O+ P+ Q+ R+ S+ T+ U+ V+ W+ X+ Y+ Z+ AA+ AB+ AC+ AD+ AE+ AF+ AG+ AH+ AI+ AJ+ AK+ AL+ AM+ AN+ AO+ AP+ AQ+ AR+...
AS+ AT+ AU+ AV;
if Total >Total_FilterConstraint
break;
end
Filter_Combos = {A; B; C; D; E; F; G; H; I; J; K; L; M; N; O; P; Q; R; S; T; U; V; W; X; Y; Z; AA; AB; AC; AD; AE; AF; AG; AH; AI; AJ; AK;...
AL; AM; AN; AO; AP; AQ; AR; AS; AT; AU; AV};
Filter_Transmittance = (Filterdata.Glass.^A).*(Filterdata.BG40.^B).*(Filterdata.BG50.^C).*(Filterdata.B370.^D).*(Filterdata.EB390.^E).*(Filterdata.B410.^F).*...
(Filterdata.B440.^G).*(Filterdata.B460.^H).*(Filterdata.G530.^I).*(Filterdata.G533.^J).*(Filterdata.G545.^K).*(Filterdata.G550.^L).*...
(Filterdata.LA20.^M).*(Filterdata.LA40.^N).*(Filterdata.ELA80.^O).*(Filterdata.ELA100.^P).*(Filterdata.ELA120.^Q).*(Filterdata.ELA140.^R).*...
(Filterdata.ELA200.^S).*(Filterdata.LB20.^T).*(Filterdata.LB40.^U).*(Filterdata.LB80.^V).*(Filterdata.LB100.^W).*(Filterdata.LB120.^X).*...
(Filterdata.LB140.^Y).*(Filterdata.LB200.^Z).*(Filterdata.C500S.^AA).*(Filterdata.C5000.^AB).*(Filterdata.CD700.^AC).*(Filterdata.CM700.^AD).*...
(Filterdata.L37.^AE).*(Filterdata.UV28N.^AF).*(Filterdata.UV30N.^AG).*(Filterdata.ND03.^AH).*(Filterdata.ND1.^AI).*(Filterdata.ND3.^AJ).*...
(Filterdata.ND5.^AK).*(Filterdata.ND10.^AL).*(Filterdata.ND13.^AM).*(Filterdata.ND15.^AN).*(Filterdata.ND20.^AO).*(Filterdata.ND25.^AP).*...
(Filterdata.ND30.^AQ).*(Filterdata.ND40.^AR).*(Filterdata.ND50.^AS).*(Filterdata.ND60.^AT).*(Filterdata.ND70.^AU).*(Filterdata.ND80.^AV).*(0.96^2);
T_Calc = [T_Calc, Filter_Transmittance];
Index = [Index, Filter_Combos];
end % AV for loop
I ran into problems when I increased the total filters to 8 and duplicates of the same filters to 4. It ran for a whole week straight before I had to halt the process and the whole program crashed and said it ran out of memory when I tried to copy the data to excel.
Is there a way to make this faster and not so unstable?
It had completed ~750k calculations when i paused it and it crashed.
There are other sections that do least squares, sorting, and finally export to excel but I couldn't even get to those sections.
0 Comments
Answers (1)
Sulaymon Eshkabilov
on 6 Nov 2021
For most of calcs in your code can be done directly without [for .. end] loops, e.g.:
A= 0:Ind_FilterConstraint;
B=A;
C=B;
% for A= 0:1:Ind_FilterConstraint
%
% for B= 0:1:Ind_FilterConstraint
% Total = A+ B;
% if Total > Total_FilterConstraint
% break;
% end
% for C= 0:1:Ind_FilterConstraint
% Total = A+ B+ C;
% if Total > Total_FilterConstraint
% break;
% end
Total = A+B+C;
...
SImilarly, you can simplify the rest.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!