Reduce processing time for the simulation
显示 更早的评论
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);
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Code Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!