Making script execution go faster
이전 댓글 표시
I have 36 similar scripts. The output of the scripts is how much the electricity consumption increases if a BEV is introduced. The combinations of households are investigated.
Script 1: the increase for one household
Script 2: the Increase for 2 households (if 2 households are randomly combined)
.
.
Script 36: The increase for 36 households (if 36 households are randomly combined(.
My problem is that the scripts are running very slowly. The execution is basically stuck.
I have a lot of for loops in the scripts. The higher number of the scripts, the more for loops. I wonder if there is a way to make the script faster?
I paste Script 3 as an example here. It combines every vehicle (Vehicle1,2,3) with every combination of household (Hcombos).
First it assigns vehicles to households (if there is 3 or more inhabitants there can be 2 vehicles)
for j = 1:nBEV
.
.
then it finds all the doubkes and removing them (i.e. two households cannot have the same car).
for jj3=1:numel(VID3)
.
.
VID1={}; %ID=Vehicles ID number
VID2={};
VID3={};
Vehicle1={};
Vehicle2={};
Vehicle3={};
IDcell2={};
GUD={};
for i2=1:length(Hcombos(:,1))
%State combinations
C=Hcombos(i2,1);
C2=Hcombos(i2,2);
C3=Hcombos(i2,3);
INCREASE2={};
HH2=Household(:, C)+Household(:, C2)+Household(:,C3);
for j = 1:nBEV
%If the household has three or more inhabitants then there can be
%two vehicles
if HHPerson(C)>=x
for k=1:nBEV
%There is only one car if
if k==j
Vehicle1{j,k}=BEV(:, j);
VID1{j,k}=ID(j);
else
Vehicle1{j,k}=BEV(:,k)+BEV(:,j);
VID1{j,k}=[ID(j) ID(k)];
end
end
end
if HHPerson(C2)>=x
for k2=1:nBEV
%There is only one car if
if k2==j
Vehicle2{j,k2}=BEV(:, j);
VID2{j,k2}=ID(j);
else
Vehicle2{j,k2}=BEV(:,k2)+BEV(:,j);
VID2{j,k2}=[ID(j) ID(k2)];
end
end
end
if HHPerson(C)<x
for k1=1:nBEV
%There is only one car if
if k1==j
Vehicle1{j,k1}=BEV(:, j);
VID1{j,k1}=ID(j);
else
Vehicle1{j,k1}=BEV(:,k1);
VID1{j,k1}=[ID(k1)];
end
end
end
if HHPerson(C2)<x
for k22=1:nBEV
%There is only one car if
if k22==j
Vehicle2{j,k22}=BEV(:, j);
VID2{j,k22}=ID(j);
else
Vehicle2{j,k22}=BEV(:,k22);
VID2{j,k22}=[ID(k22)];
end
end
end
%3----------------------------
if HHPerson(C3)>=x
for k3=1:nBEV
%There is only one car if
if k3==j
Vehicle3{j,k3}=BEV(:, j);
VID3{j,k3}=ID(j);
else
Vehicle3{j,k3}=BEV(:,k3);
VID3{j,k3}=[ID(k3)];
end
end
end
if HHPerson(C3)<x
for k33=1:nBEV
%There is only one car if
if k33==j
Vehicle3{j,k33}=BEV(:, j);
VID3{j,k33}=ID(j);
else
Vehicle3{j,k33}=BEV(:,k33);
VID3{j,k33}=[ID(k33)];
end
end
end
end
for jj3=1:numel(VID3)
V3=cell2mat(Vehicle3(jj3));
ID3=cell2mat(VID3(jj3));
for jj=1:numel(VID1)
V1=cell2mat(Vehicle1(jj));
ID1=cell2mat(VID1(jj));
if numel(intersect(ID1,ID3))
continue
end
for jj2=1:numel(VID2)
V2=cell2mat(Vehicle2(jj2));
ID2=cell2mat(VID2(jj2));
%Removes all doubles
if numel(intersect(ID1,ID2))
continue
end
INCREASE2{jj,jj2}=max(HH2+V1+V3+V2)./max(HH2);
end
end
end
GUD{i2}=INCREASE2;
%HUSGUD{i2}=[C(i2) CC(i2)];
end
for lol = 1:numel(GUD)
idx = ~cellfun(@isempty,GUD{lol});
tmp = zeros(size(GUD{lol})); % or perhaps NAN.
tmp(idx) = [GUD{lol}{idx}];
GUD{lol} = tmp;
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!