Reduce the computional time to calculate the mutilplying two matrixes

조회 수: 1 (최근 30일)
Thu Nguyen
Thu Nguyen 2019년 5월 17일
댓글: Thu Nguyen 2019년 5월 17일
I calculate this loop for thousands of time.
How can i optimize it to reduce the computational time?
for i=1:n
f(i,:)=w'*Q0(:,:,i)
end
with w =(2000,1) matrix and Q0=(2000,2000,100)
Thanks a lot.
  댓글 수: 2
dpb
dpb 2019년 5월 17일
  1. Preallocate f -- f=zeros(n,numel(w));
  2. Reorient w outside the loop
The first may help enough to be noticeable if haven't; the second is very minor aid to the optimizer and likely will make little, if any, difference.
Thu Nguyen
Thu Nguyen 2019년 5월 17일
Thank you a lot. I also made preallocate and reorient as you made and only reduce little time. I need reduce more such as ~ 100 times.

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

답변 (1개)

David Goodmanson
David Goodmanson 2019년 5월 17일
편집: David Goodmanson 2019년 5월 17일
Hi Thu,
try
% N = 1000;
% M = 200;
Q00 = reshape(Q0,N,N*M);
ff = reshape(w'*Q00,N,M)';
This is approximately four times faster on my PC, compared to the first way with f preallocated.
  댓글 수: 1
Thu Nguyen
Thu Nguyen 2019년 5월 17일
Thank you so much. I found this solution very interesting. But on my PC, it reduce time a little. I want to reduce the computational time about 100 times. Any solution?

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

카테고리

Help CenterFile Exchange에서 Robust Control Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by