Code optimization for nested for loop
Show older comments
Hi, I am a newbie and I need to implement following code for 1000 beta value. I can not use nested for loop. I request to you please suggest some method to remove nested for-loop.
%% Start Session
clc;
clear all;
close all;
warning off;
tStart = tic;
ScoreTolren=-0.25;
iterations = 0;
SeqInNorm = [];
indNorm = 0;
SEqNor4Check = [];
CheckEntry =0;
ExitCondition = 0;
MaxItr =0;
step = 1/8;
IntialLoop = 0;
for beta1 = IntialLoop:step:1
for beta2 = IntialLoop:step:1-beta1
for beta3 = IntialLoop:step:1-beta1-beta2
for beta4 = IntialLoop:step:1-beta1-beta2-beta3
for beta5 = IntialLoop:step:1-beta1-beta2-beta3-beta4
for beta6 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5
for beta7 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5-beta6
for beta8 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5-beta6-beta7
for beta9 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8
for beta10 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8-beta9
for beta11 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8-beta9-beta10
for beta12 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8-beta9-beta10-beta11
for beta13 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8-beta9-beta10-beta11-beta12
for beta14 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8-beta9-beta10-beta11-beta12-beta13
for beta15 = IntialLoop:step:1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8-beta9-beta10-beta11-beta12-beta13-beta14
beta16 = 1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8-beta9-beta10-beta11-beta12-beta13-beta14-beta15;
iterations = iterations+1;
b(iterations,:)= [beta1,beta2,beta3,beta4,beta5,beta6,beta7,beta8,beta9,beta10,beta11,beta12,beta13,beta14,beta15,beta16];
if iterations == 1e5
ExitCondition = 1;
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
if ExitCondition == 1
break;
end
end
%% End of iteration
elapsedTime = toc(tStart);
1 Comment
Jan
on 24 Jan 2019
I've formatted your code today. Please do this by your own in teh future: Select the code, press the button for code formatting. Do not press "Alt-Enter", as displayed in the tool tip, but "Strg+E"
Start with omitting this evil header:
clc;
clear all;
close all;
warning off;
Disabling all warnings is an extremely bad idea and a shot in your knee. There is not reason to call clear all, which removes all loaded functions from the memory. The reloading from the slow disk wastes a lot of time.
Accepted Answer
More Answers (1)
Letting an array grow iteratively wastes a lot of time. Pre-allocate the output.
[EDITED: Code fixed:]
function main
tStart = tic;
b = Method2();
elapsedTime = toc(tStart);
end
function b = Method2()
nIter = 490314;
iter = 0;
step = 1/8;
i0 = 0;
b = zeros(nIter, 16);
for b1 = i0:step:1
for b2 = i0:step:1-b1
for b3 = i0:step:1-b1-b2
for b4 = i0:step:1-b1-b2-b3
bb14 = b1 + b2 + b3 + b4; % Just for readability
for b5 = i0:step:1-bb14
for b6 = i0:step:1-bb14-b5
for b7 = i0:step:1-bb14-b5-b6
for b8 = i0:step:1-bb14-b5-b6-b7
bb18 = bb14 + b5 + b6 + b7 + b8;
for b9 = i0:step:1-bb18
for b10 = i0:step:1-bb18-b9
for b11 = i0:step:1-bb18-b9-b10
for b12 = i0:step:1-bb18-b9-b10-b11
bb112 = bb18 + b9 + b10 + b11 + b12;
for b13 = i0:step:1-bb112
for b14 = i0:step:1-bb112-b13
for b15 = i0:step:1-bb112-b13-b14
b16 = 1-bb112-b13-b14-b15;
iter = iter+1;
b(iter,:) = [b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16];
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
Less ugly, but not really nice.
This takes 1.6 seconds instead of 37 on my machine.
Categories
Find more on Loops and Conditional Statements 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!