Can this For loop be converted into matrix operation?

Here`s my code. Can this for loop at the start be converted into matrix operation as it`d save a lot of time then.
function [TTC,CL]=ttccl_EC(n,m,w,s,M)
mxt=[];
cl=[];
for t=1:M
Ticomp=exprnd(1,1,n);
Ticomm=0.1;
T=Inf(n,1);
cltime=[];
for i=1:n
p=0;
for j=i:i+m-1
T1=(w+p)*Ticomp(i)+Ticomm;
cltime=[cltime T1];
p=p+1;
if mod(j,n)~=0
T(mod(j,n))=min(T(mod(j,n)),T1);
else
T(n)=min(T(n),T1);
end
end
end
Ts=sort(T);
maxt=Ts(n-s);
end
cl=[cl length(cltime(cltime<=maxt))];
mxt=[mxt maxt];
%% Total time until the full sum is determined %%
TTC=mean(mxt);
%% Measures the number of messages received by the fusion node when TTC is met %%
CL=mean(cl);
end

1 commentaire

I make no claims about whether it can be vectorized, but you can make it about 20x faster if you preallocate cltime instead of growing it by concatenation.
cltime = zeros(1,m*n);
Of course you only really need to do that inside the outer loop. The first instance is redundant.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Commenté :

DGM
le 10 Avr 2021

Community Treasure Hunt

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

Start Hunting!

Translated by