I need to optimize my matlab code through vectorization
1 次查看(过去 30 天)
显示 更早的评论
I need to vectorize this loop in code.. can someone guide?
function X = IDWT2DCT(x,Dct_matrix,IW_matrix)
[K,MN]=size(x);
block_size = 8;
X = zeros(block_size,block_size,K);
temp = zeros(block_size,block_size);
y = (Dct_matrix'*x);
for k = 1:K
temp = reshape(y(k,:),block_size,block_size);
X(:,:,k) = IW_matrix*temp*IW_matrix';
end
*****Speed is not issue, i just need to vanish this loop****** [EDITED, Jan, Code formatted]
6 个评论
Image Analyst
2015-4-16
I agree with Jan that the loop takes virtually no time, unless K is huge, and I'm talking billions. Looping a few hundred thousand or a few million times takes like a microsecond. What is the value of K? What are the dimensions of Y and x?
回答(1 个)
Jan
2015-4-16
I hesitate to invest any period of time when I cannot see any benefit. You ask for an "optimization", but the code will not be better in any way if it is vectorized. Therefore I'm convinced, that I cannot "optimize" it, especially when speed does not matter. If you want to have more complex for debugging and most likely faster as a side-effect, try these marvelous functions:
- http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support
- http://www.mathworks.com/matlabcentral/fileexchange/37515-mmx-multithreaded-matrix-operations-on-n-d-matrices
Please note, that "optimizing" code, which is not a bottleneck of a program is a known anti-pattern of programming. See http://en.wikipedia.org/wiki/Anti-pattern -> "premature optimization" and http://en.wikipedia.org/wiki/Optimization_(computer_science)#When_to_optimize
2 个评论
James Tursa
2015-4-16
编辑:James Tursa
2015-4-16
If you are implementing this on a GPU then why not work with a BLAS library to do the matrix multiplies? E.g.,
https://developer.nvidia.com/cublas
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!