Nested for loops taking a long time. Any tips in how to make it faster?
2 views (last 30 days)
Show older comments
The first double loop works fine. But the next nested loop is taking such a long time, after 6 hours I came about 10 %. The code seem to work although(tested with smaller nested loop) Anything I can do to make the nested loop go faster?
%%%%%%%%%%%%%%%%%%%%%%%%Making the right rows into cell 1x406 with 8x3 matrix in each%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rows=num(1:end,4:11); %406x8 double
nr_of_rows=length(rows); %406
row_matrix=zeros(8,3); %8x3 double
right_row_matrix=cell(1,nr_of_rows); %1x406 cell
hit_matrix = cell(7,7,7,7,7,7,7,7); %8-D cell
for g=1:nr_of_rows
for h=1:8
if (rows(g,h))==1
row_matrix(h,:)=[1 4 4];
elseif (rows(g,h))==3
row_matrix(h,:)=[4 3 4];
elseif (rows(g,h))==2
row_matrix(h,:)=[4 4 2];
end
right_row_matrix{1,g}=row_matrix;
end
end
%%%%%%%%%%%%%%%%%%%%%%%Creating all possible combinations and checking against right_row_matrix%%%%%%%%%%%%%%%%%%%%%%%%%%
possible_values=[1 3 2;1 3 0;1 0 2; 0 3 2;1 0 0;0 3 0;0 0 2];
combn_matrix = cell(7,7,7,7,7,7,7,7);
cost_matrix=zeros(7,7,7,7,7,7,7,7);
temp3=zeros(1,8);
temp=zeros(8,3);
tot_hitrate_matrix=zeros(7,7,7,7,7,7,7,7);
tot_win_matrix=zeros(7,7,7,7,7,7,7,7);
amount_winnings=num(1:end,3); %406x1 double
h1 = waitbar(0,'i:Please wait...');
for a = 1:7
for b = 1:7
for c = 1:7
for d = 1:7
for e = 1:7
for f = 1:7
for g = 1:7
for h = 1:7
combn_matrix{a,b,c,d,e,f,g,h}=[possible_values(a,:);possible_values(b,:);possible_values(c,:);...
possible_values(d,:);possible_values(e,:);possible_values(f,:);...
possible_values(g,:);possible_values(h,:)]; %Creates 7^8 different 8x3 matrices and stores them in comb_matrix
temp=combn_matrix{a,b,c,d,e,f,g,h};
for j=1:length(temp)
if sum(temp(j,:)==0)==0
temp3(1,j)=3; %Add cost 3 if condition is met
elseif sum(temp(j,:)==0)==1
temp3(1,j)=2; %Add cost 2 if condition is met
elseif sum(temp(j,:)==0)==2
temp3(1,j)=1; %Add cost 1 if condition is met
end
end
cost_matrix(a,b,c,d,e,f,g,h)=prod(temp3); %Adding the total cost for each specific matrix
for i=1:nr_of_rows %nr_of_rows is at the moment 406, and I think the bottleneck is here(?)
check=right_row_matrix{1,i}==temp; %Checking 1 specific matrix(temp) against each matrix right_row_matrix{1,i}
if sum(sum(check,2))==3
j=0;
tot_hitrate_matrix(a,b,c,d,e,f,g,h)=tot_hitrate_matrix(a,b,c,d,e,f,g,h)+(j+1); %Accumulate how many times condition is met
tot_win_matrix(a,b,c,d,e,f,g,h)=tot_win_matrix(a,b,c,d,e,f,g,h)+amount_winnings(i); %Accumulate total winnings when condition is met
end
end
end
end
end
end
end
end
end
waitbar(a/7)
end
close(h1)
3 Comments
Stephen23
on 9 Sep 2015
Edited: Stephen23
on 9 Sep 2015
@André Svensson: not many people are going to sit and reverse-engineer such a large block of code. Consider the situation: combined we have a lot of MATLAB experience, but what we don't know is what you are trying to do, so this is what you need to explain to us (exactly as Hamoon said too). If you tell us exactly what data you have and what you need to achieve than we can give you advice and show you how to do it.
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!