Can matlab not perform 115880 iterations fast?its taking hours to save the result of all iterations..how to iterate over all combinations and get results in minimum time

1 view (last 30 days)
summyia qamar
summyia qamar on 12 Jan 2017
Answered: Steven Lord on 13 Jan 2017
I have an array all_comb_ofRoutes=<1x64 cell> and onother array allCell_array=<1x1806 cell> I am doing multiplication of each elements using this code
[r2,c2]=size(all_comb_ofRoutes)
count=1;
for m=1:c2
for n=1:numel(allCell_array)
movement=(all_comb_ofRoutes{m})*allCell_array{n};
movement=movement>0;
total_movement=sum((sum(movement,2))-1);
count=count+1;
total(m:n)=total_movement
end
end
minValue=min(total)
[all_comb_ofRoutes(m) combination_array(n) allCell_array(n) minValue]
I want to store values of all iterations to get the minimum out of them but its taking very long time. how can I speed up this code? anyone please suggest me improvements
the complete code is given below
tic;
no_of_machines=7;
no_of_cells=3;
No_of_Parts=6;
P1=[1 0 0 1 0 1 1];
P2=[0 1 1 1 0 0 1];
P3=[1 0 0 1 1 0 0];
P4=[1 0 0 0 1 0 1];
P5=[1 1 0 0 0 1 0;1 1 0 0 1 0 1];
P6=[0 1 0 0 0 1 1;1 1 0 1 0 1 0];
P=vertcat(P1,P2,P3,P4,P5,P6);
n= 2^2-1;
all_comb_ofRoutes = zeros(No_of_Parts,size(P1,2),n+1);
for i=0:n
all_comb_ofRoutes(:,:,i+1) = P((1:1:6)+(dec2bin(i,No_of_Parts)-'0'),:);
end
all_comb_ofRoutes = cell(1,n+1);
for j=0:n
all_comb_ofRoutes{j+1} = P((1:1:6)+(dec2bin(j,No_of_Parts)-'0'),:);
end
C = zeros(no_of_cells^no_of_machines,no_of_machines);
t = 0;
for k = 0:(no_of_cells^no_of_machines)-1
s = dec2base(k,no_of_cells,no_of_machines);
if length(unique(s))==no_of_cells
t = t+1;
C(t,:) = s-'0'+1;
end
end
C = C(1:t,:);
combination_array=num2cell(C,2);% all possible combinations
%generating machine/cell matrix for above combinations
[r1,c1]=size(combination_array)
for l=1:r1
R=1:numel(combination_array{l});
Z = zeros(R(end),max(combination_array{l}));
Z(sub2ind(size(Z),R,combination_array{l})) = 1;
allCell_array{l}=Z; % machine cell matrix array of whole population
end
%calculating total movements
[r2,c2]=size(all_comb_ofRoutes)
count=1;
for m=1:c2
for n=1:numel(allCell_array)
movement=(all_comb_ofRoutes{m})*allCell_array{n};
movement=movement>0;
total_movement=sum((sum(movement,2))-1);
count=count+1;
total(m:n)=total_movement
end
end
minValue=min(total)
[all_comb_ofRoutes(m) combination_array(n) allCell_array(n) minValue]
toc;
I need help in changing code for last potion % calculating total movements

Answers (2)

Jan
Jan on 13 Jan 2017
  • Omit the useless "count=count+1;"
  • Do you really mean total(m:n)=total_movement, or total(m, n) = ...?
  • Is total pre-allocated? If not, search in this forum for "pre-allocation".
total = zeros(c2, numel(allCell_array));
for m = 1:c2
for n = 1:numel(allCell_array)
movement = all_comb_ofRoutes{m} * allCell_array{n};
total(m, n) = sum(sum(movement > 0, 2)) - size(movement, 1);
end
end
  2 Comments
summyia qamar
summyia qamar on 13 Jan 2017
sir this SUM((SUM(MOVEMENTS>0,2)-1) is my objective function which means 1 minus all the movements why you replaced it with size(movement,1)? have to run this code in your software?

Sign in to comment.


Steven Lord
Steven Lord on 13 Jan 2017
Run your code in the MATLAB Profiler using a subset of the data from your larger cell arrays -- for instance, run your code using the first 10 or 20 cells in each cell array. Use the Profiler output (and any suggestions provided by Code Analyzer in the Editor window) to identify the bottlenecks in your code and potentially ways to improve those bottlenecks.

Community Treasure Hunt

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

Start Hunting!