How to vectorize this for loop

1 回表示 (過去 30 日間)
Leo Tu
Leo Tu 2021 年 7 月 5 日
コメント済み: Leo Tu 2021 年 7 月 5 日
I have the following script which I have managed to test for up to t=200 however I need it to go up to t=12054 and it takes too long to run.
Could anyone please help with vectorizing the code so that it can run faster? I have attached my data T
% T is a column of temperature data over 12054 days
V = flip(T,1)
syms k;
for t=1:12054
L(t) = 1+symsum(.5.^(abs(k-5)*V(t)),k,0,12054-t)/symsum(.5.^abs(k-5),k,0,12054-t);
end
lambda = double(L);
lambda = flip(lambda,2) % flipping back because we initially flipped T to get V.
r = poissrnd(lambda);
plot(r)

採用された回答

Paul
Paul 2021 年 7 月 5 日
編集済み: Paul 2021 年 7 月 5 日
For starters, why use symsum at all? Doesn't sum() do exactly what is needed?
for t = 1:12054
k = 0:(12054 - t);
L(t) = 1 + sum(0.5.^(abs(k-5).*V(t)))./sum(0.5.^(abs(k-5)));
end
lambda = flip(L,2);
I suspect other optimizations can be made as well. For sure, pre-allocate L. But for only 12054 elements, this loop may be fast enough for what's needed.
  1 件のコメント
Leo Tu
Leo Tu 2021 年 7 月 5 日
Thank you @Paul that's what I needed!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by