- Preallocate f -- f=zeros(n,numel(w));
- Reorient w outside the loop
Reduce the computional time to calculate the mutilplying two matrixes
1 次查看(过去 30 天)
显示 更早的评论
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
2019-5-17
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.
回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Robust Control Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!