필터 지우기
필터 지우기

Low computation efficiency of simple sentence in MATLAB

조회 수: 2 (최근 30일)
Albert
Albert 2016년 10월 20일
편집: Thorsten 2016년 10월 21일
Hi, everyone
I have an computation efficiency problem here, I wish someone could help me out.
here is part of my MATLAB code (the actual code is much bigger than the following):
lambda_new1=ones(32*32*32,1);
W1=zeros(32*32*32,t);
W2=zeros(32*32*32,t);
W3=zeros(32*32*32,t);
W4=zeros(32*32*32,t);
temp=reshape(sum(repmat(lambda_old1,1,4).*[W1(:,i),W3(:,i),W2(:,i),W4(:,i)]),2,2);
How come that the last sentence in above code is running so slow, which accounts for 35% total running time? Actually my code is much bigger than the above and there are more complicated computation, but why such a short and concise computation takes so much time?
Is there any better way to improve computation efficiency?
Also the line numbers in my MATLAB (2016a and 2016b) would disappear after a while, if I relaunch the software, the line number would come back. Is this an intrinsic bug in MATLAB? How can I fix it?
Greatly appreciate your help
Thank you
Albert Liew

채택된 답변

Thorsten
Thorsten 2016년 10월 20일
편집: Thorsten 2016년 10월 21일
Instead of
X = repmat(lambda_old1,1,4).*[W1(:,i),W3(:,i),W2(:,i),W4(:,i)];
if might be faster to use
X = bsxfun(@times, [W1(:,i),W3(:,i),W2(:,i),W4(:,i)], lambda_old1);
  댓글 수: 2
Albert
Albert 2016년 10월 20일
Hi, Thorsten
Thank you very much for your answer. It does work much faster. The reason why is that, I guess, by using 'bsxfun', we simply circumvent the process of copying and pasting a large matrix, which is time consuming.
Thank you again.
Thorsten
Thorsten 2016년 10월 21일
Exactly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by