Reduce computation time in the for-loop
1 view (last 30 days)
Show older comments
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 Comment
dpb
on 5 Sep 2018
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.
Answers (0)
See Also
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!