Fast Element-Wise power

13 次查看(过去 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
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 CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by