Reduce computation time in the for-loop

1 次查看(过去 30 天)
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