MATLAB: Code optimization for nested for loop

optimization of nested-for loop

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);

Best Answer

Instead of implementing the nested loop by hard coding, you can use a vector as iteration counter:
function Result = Method1
nIter = 490314;;
i0 = 0;
n = 16;
Result = zeros(n, nIter);
b = [zeros(n-1, 1); 1];
step = 1/8;
for k = 1:nIter
Result(:, k) = b;
% Increase index vector b:
b(n) = 0;
for ib = n-1:-1:1
if sum(b) <= 1 - step
b(ib) = b(ib) + step;
break; % Update of b has finished
end
b(ib) = i0; % reset current b
end
b(n) = 1 - sum(b(1:15));
end
Result = Result.';
Please test and adjust this to your needs. This is about 50% slower than the code in my other answer Method2, but nicer.
Accesing the Result columnwise instead of rowwise allows a better usage of the processor cache.
By the way: This code obtains all row vectors with 16 elements, which contains elements of teh set 0:1/8:1, which have the sum of 1.0.