Reduce computation time in the for-loop

조회 수: 1 (최근 30일)
Jeongseop Lee
Jeongseop Lee 2018년 9월 5일
댓글: dpb 2018년 9월 5일
Hi all,
I have three "n-by-n" matrices and one "m-by-4*n" matrix. A for-loop is used to compute a scalar for each row in X for all m and stores them in a m-by-1 matrix in the following manner.
% declare variables for this particular example with random parameters.
n = 100; m = 100; D = zeros(m,1);
A = random('norm',0,1,[n,n]);
B = random('norm',0,1,[n,n]);
C = random('norm',0,1,[n,n]);
X = random('norm',0,1,[m,4*n]);
for i = 1 : m
D(i)=X(i,3*n+1:4*n)*A*toeplitz(X(i,1:n))*B*toeplitz(X(i,n+1:2*n))*X(i,2*n+1:3*n)';
end
Typically my n and m are around 1e3 and I have to iterate this for-loop about 1e6 times for topological optimization so the computation takes quite a bit of time. One way I see forward is to to calculate D without the for-loop somehow. Any suggestions as to how to proceed with this?
Thanks,
  댓글 수: 1
dpb
dpb 2018년 9월 5일
At the purely mechanistic it appears would just be replacing an explicit for loop with arrayfun that likely woudn't be as fast, anyway.
I'm guessing the only way would be to sit down with paper/pencil and see if can do something with the algebra to reduce it somehow. In the real case are there any patterns that can exploit; if everything is random as here, that pretty-much negates that, too.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by