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

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 Exchange에서 Loops 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