How do I speed up loops in the code?

1 Ansicht (letzte 30 Tage)
RP
RP am 2 Feb. 2023
Bearbeitet: DGM am 2 Feb. 2023
I have the following code which consists of three loops whicjh is taking a long time to run. Any ideas how to speed it up?
C=ones(NumSims, NumScen);
for i=1:NumScen
B=copularnd('Gaussian',PSDMatrix,NumSims);
C(:,i)=icdf('poisson',B(:,i),A(i,1));
M=cumsum(C);
Maxsim=M(end,:);
for i=1:NumScen
Col(1,i)=max(C(:,i));
Col2=max(Col);
LossFlag=zeros(NumSims,Col2);
end
for i=1:NumScen
for j=1:NumSims
for k=1:Col2
if k<=C(j,i)
LossFlag(j,k,i)=1;
else
0;
  3 Kommentare
RP
RP am 2 Feb. 2023
NumSims=10000
NumScen = 25
Col2=20
DGM
DGM am 2 Feb. 2023
Bearbeitet: DGM am 2 Feb. 2023
Since the pasted code is incomplete, I can only guess what's missing. I see a bunch of nested and/or improperly terminated loops. There appear to be two 1:NumScen loops that run inside of another for no apparent reason. I suspect that this can be simplified, but not without knowing what's missing.
C=ones(NumSims, NumScen);
for i=1:NumScen % everything is inside this loop
B=copularnd('Gaussian',PSDMatrix,NumSims);
C(:,i)=icdf('poisson',B(:,i),A(i,1));
% these get overwritten each time, but aren't apparently used
% if they aren't used, get rid of them
% if they can be calculated outside the loop, move them outside the loop
% if only Maxsim is needed, get rid of M
% Maxsim = sum(C,1);
M=cumsum(C);
Maxsim=M(end,:);
for i=1:NumScen % ... so this loop appears redundant or misplaced
% ... but idk if Col needs to be fully written
% if so, this could just be vectorized -- but idk what size Col is
% if Col is only used to calculate Col2, then this simplifies
Col(1,i)=max(C(:,i)); % i'm assuming Col is a vector?
Col2=max(Col); % ambiguous dimension; again, suggests Col is a vector
LossFlag=zeros(NumSims,Col2); % not preallocated to the size used later
end
for i=1:NumScen % ... and this loop appears redundant or misplaced
for j=1:NumSims
for k=1:Col2
% this should be able to be reduced to a single relational
% test for the entire array, without any loops needed
if k<=C(j,i)
LossFlag(j,k,i)=1;
else
0; % this does nothing
end % missing??
end % missing??
end % missing??
end % missing??
end % missing??
Perhaps something like this? (not tested)
% populate C
C=ones(NumSims, NumScen);
for i = 1:NumScen % everything is inside this loop
B = copularnd('Gaussian',PSDMatrix,NumSims);
C(:,i) = icdf('poisson',B(:,i),A(i,1));
end
% assuming this can be moved and that M is not needed itself
Maxsim = sum(C,1);
% assuming Col is only needed to calculate Col2
Col2 = max(C,[],'all');
k = 1:Col2; % a row vector
LossFlag = k <= permute(C,[1 3 2]); % compare to generate a 3D logical array
These are a lot of gross assumptions on my part, but that's all I can do.
Consider the example:
outvector = zeros(1,10); % a test vector
n = 0;
for k = 1:10
for k = 1:10 % trying to alter loop index within a loop is bad
% this runs 100 times, not 10
n = n+1;
outvector(k) = n;
end
end
% 90% of all the results are just discarded
outvector
outvector = 1×10
91 92 93 94 95 96 97 98 99 100

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by