Fast Element-Wise power

10 ビュー (過去 30 日間)
Thomas Bauer
Thomas Bauer 2017 年 10 月 30 日
Hello all,
I am trying to optimize some code that contains element wise calculations with rather large matrices. I have already shaved off about 50% with things like the bsxfun (which in my case works wonders). But now I am stuck at an operation where a simple line of code value=matrix.^3 takes up most of the runtime of the script. is there any way to perform this element-wise calculation faster?
Best regards
  1 件のコメント
Thomas Bauer
Thomas Bauer 2017 年 10 月 30 日
Hello, i think i just answered my own question. Appearently using
value=matrix.*matrix.*matrix
runs significantly faster.
BTW i also figured out bsxfun was NOT the reason for my faster running script. It actually was the termination of several operations by precalculating another variable.

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

採用された回答

KL
KL 2017 年 10 月 30 日
編集済み: KL 2017 年 10 月 30 日
a=1:10000000;
b=repmat(3,size(a));
tic
c1=a.^b;
toc
tic
c2=a.^3;
toc
tic
c3=a.*a.*a;
toc
And then the results are,
Elapsed time is 0.118509 seconds.
Elapsed time is 0.129281 seconds.
Elapsed time is 0.006726 seconds.
  2 件のコメント
Thomas Bauer
Thomas Bauer 2017 年 10 月 30 日
Thank you for this example!
Johannes Kalliauer
Johannes Kalliauer 2019 年 4 月 1 日
sometimes if you have small integers you can speed it up by useing integers
a=randi([0,6],1,10000000);
b=repmat(3,size(a));
int8a=uint8(a);
tic
c1=a.^b;
toc
tic
c2=a.^3;
toc
tic
c3=a.*a.*a;
toc
tic
c4=int8a.*int8a.*int8a;
toc
And then the results are,
Elapsed time is 0.199174 seconds.
Elapsed time is 0.153210 seconds.
Elapsed time is 0.014326 seconds.
Elapsed time is 0.002569 seconds.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by