Reduce processing time for the simulation
Afficher commentaires plus anciens
Hi, i have the below-mentioned code and it is taking 25 minutes for 1000000 iterations. Can someone suggest the way to optimize the total processing time? I think the for loop in line 23 is taking the major processing time. Is there any way to optimize it.
V= 0.002;
C=0.002;
I= eye(221);
s = string({'CR';'E';'R';'S';'SR'});
[x,y] = ndgrid(1:20,[2,5]);
str = s([2;3;2;4;y(:)]) + ([101;1;106;1;x(:)] + (0:4));
str = cellstr([s(1),str(:)']);
d = {tril(ones(44,4),-1),diag(ones(20,1),-24)};
d{1}(4:end,3) = 0;
d{2} = d{2}(:,1:end-4);
dd = repmat({[d{:}]},1,5);
dd = [zeros(1,221);[ones(220,1),blkdiag(dd{:})]];
out = [{nan},str;str(:), num2cell(dd) ];
A= cell2mat(out(2:end,2:end));
result=zeros(221,4,1000);
for i=1:10
A1= 0 + (1-0).*rand(221,1);
X11= inv(I-(A))*A1;
P= zeros(221,1);
P(:)= X11(:)/sum(X11);
D1= 0 + (1-0).*rand(221,1);
Utility=0;
for j=1:221
Utility= (((1-P(j))*(D1(j)/(D1(j)+A1(j)))* V)-C) +Utility;
end
result(:,1,i)=A1;
result(:,2,i)=D1;
result(:,3,i)=P;
result(1,4,i)=Utility;
end
[Utility_max,index_max]=max(result(1,4,:));
A1_max=result(:,1,index_max);
D1_max=result(:,2,index_max);
P_max=result(:,3,index_max);
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Code Performance dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!