Code optimization for 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);

1 Commento

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.

Accedi per commentare.

 Risposta accettata

Jan
Jan il 24 Gen 2019
Modificato: Jan il 24 Gen 2019
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.

4 Commenti

Hi, Thanking you very much for your help. I will run it and try to resolve my problems.
Hi Jan, I have run your code. I need all elements with in (0:1/8:1) for my research. But after running your code, i am getting from 0 to 0.5 in each column means we have missed [0.625, 0.75,0.875,1]. I have changed in line no. 12 in if condition with 8*step i.e.(if b(ib) < 1 - sum(b(1:ib)) + 8*step) . But after doing this change, sum of individual row is exceeding from 1. Please suggest some change sothat i can get all collections with sum <=1 for individual row.
Thanking you Jan.
Jan
Jan il 24 Gen 2019
Modificato: Jan il 24 Gen 2019
@anil Tiwari: It would be useful, if you explain the purpose of the code exactly, because this increases the chance to find an efficient solution. Do I understand correctly: You want all combinations with 16 elements and values of the set 0:1/8:1, which have the sum ==1.0 or <= 1.0?
Your code:
beta16 = 1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8- ...
beta9-beta10-beta11-beta12-beta13-beta14-beta15;
looks like sum should be == 1.0. Pleae clarify this.
I've modified my code.
Hi Jan, Thanks for giving valauble time. I am basiclly generating total combination for elements of 4*4 Matrix i.e. (beta1,beta2,beta3,beta4,beta5,beta6,beta7,beta8, beta9,beta10,beta11,beta12,beta13,beta14,beta15,beta16). These elements are non-negative and sum of all elements is 1. That's why, i have used 15 nested for-loop and last element individually i.e. (beta16 = 1-beta1-beta2-beta3-beta4-beta5-beta6-beta7-beta8- beta9-beta10-beta11-beta12-beta13-beta14-beta15) s.t. sum always will be one without any repeation. I can not take random distribuiton. I have run your modified code but still i m not getting all correct possible combinations.
Please suggest some more concrete logic.

Accedi per commentare.

Più risposte (1)

Jan
Jan il 24 Gen 2019
Modificato: Jan il 24 Gen 2019
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.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by