Fast Element-Wise power

조회 수: 1 (최근 30일)
Thomas Bauer
Thomas Bauer 2017년 10월 30일
댓글: Johannes Kalliauer 2019년 4월 1일
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 CenterFile Exchange에서 Parallel Computing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by