How do I speed up loops in the code?

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 Comments

What is the value of NumScen and NumSims? Chances are that unless you have hundred of millions or billions of iterations, the for loop iteration itself is not what's slowing it down. I can do 100 million iterations on en empty for loop in 0.09 seconds, so I doubt it's the for loop itself that's the culprit. It's the stuff inside.
NumSims=10000
NumScen = 25
Col2=20
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

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2022a

Asked:

RP
on 2 Feb 2023

Edited:

DGM
on 2 Feb 2023

Community Treasure Hunt

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

Start Hunting!