Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How can i vectorize this loop?

1 回表示 (過去 30 日間)
Julien Neuhaus
Julien Neuhaus 2020 年 5 月 24 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hi, I have to compute the stucture functions of velocity increments of a velocity sample (vector v) of size 1X13'000'000. My code is the following
for li = 1:20000
vl = v(1+li:end)-v(1:end-li);
S_2(li) = mean(vl.^2);
S_3(li) = mean(vl.^3);
S_4(li) = mean(vl.^4);
end
Is it possible to vectorize this loop to speed up computation ?
Thanks for your answers
  3 件のコメント
Rik
Rik 2020 年 5 月 24 日
@William, feel free to move that to the answer section.
@Julien, I don't see a way to use a convolution or the mov___ functions, which generally would do something like this. What are you using this for? There might be a way to do a different calculation that gets you the downstream result. A change of algorithm will speed up your code much more than optimizing your code.
Julien Neuhaus
Julien Neuhaus 2020 年 5 月 25 日
@William, Thank you it works better like this.
@Rik I have to compute it in a project about turbulence in a fluid flow at university, but the solution from William is already a good optimization.
Thanks to you two

回答 (1 件)

William Alberg
William Alberg 2020 年 5 月 25 日
@Rik told me to move my comment to the answer section, i hope he ment copy-paste :)
I don't know if you can vectorize that, since vl change size every iteration
You can still speed it up a bit by doing the following:
S_2 = nan(L,1);
S_3 = nan(L,1);
S_4 = nan(L,1);
for li = 1:L
vl = v(1+li:end)-v(1:end-li);
temp = vl.^2;
S_2(li) = mean(temp);
temp = temp .* vl;
S_3(li) = mean(temp);
temp = temp .* vl;
S_4(li) = mean(temp);
end
  1 件のコメント
Rik
Rik 2020 年 5 月 25 日
Yes, I did mean copy-pasting the contents of your comment. Actually moving posts between the answer and comment section has been on the wish-list for years now, so I doubt that will be possible any time soon.

この質問は閉じられています。

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by