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 件のコメント

DGM
DGM 2021 年 4 月 10 日
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.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2021 年 4 月 9 日

コメント済み:

DGM
2021 年 4 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by