Nested for loops taking a long time. Any tips in how to make it faster?

2 vues (au cours des 30 derniers jours)
André Svensson
André Svensson le 9 Sep 2015
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 commentaires
Stephen23
Stephen23 le 9 Sep 2015
Modifié(e) : Stephen23 le 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.
André Svensson
André Svensson le 9 Sep 2015
Modifié(e) : André Svensson le 9 Sep 2015
Yes of course! Sorry for this(but now you can see the code atleast).
I want to create 5764801 different 8x3 matrices.
In each row of one of these matrices, it can be 1 out of 7 vectors. The total different combinations of matrices then becomes 7^8. I've stored them into 'combn_matrix' in my code above. Creating this doesnt take that long.
Now, after all these matrices are stored, I have a database with another 406 8x3 matrices. Basically, I want to cross-check how many of these 5764801 matrices that fulfill certain conditions with these 406 8x3 matrices (called right_row_matrix in my code). And each time it fulfills it, I want to have a another matrices that stores specific values for said matrix that fulfills the condition.
I understand if this isnt making any sense, but I'm kind of stuck. I'm totally new to vectorizing, but I suspect that is something I should do here. Reading about it right now, but I appreciate any input or links that puts me in the right direction.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by