I need to optimize my matlab code through vectorization

1 view (last 30 days)
I need to vectorize this loop in code.. can someone guide?
function X = IDWT2DCT(x,Dct_matrix,IW_matrix)
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';
*****Speed is not issue, i just need to vanish this loop****** [EDITED, Jan, Code formatted]
Abeera Tariq
Abeera Tariq on 17 Apr 2015
I need to implement the whole code on GPU, issue is that there are many loops inside loops .. so as an initiative i took inner most loop so that i can move to outer most loop..

Sign in to comment.

Answers (1)

Jan on 16 Apr 2015
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:
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
James Tursa
James Tursa on 16 Apr 2015
Edited: James Tursa on 16 Apr 2015
If you are implementing this on a GPU then why not work with a BLAS library to do the matrix multiplies? E.g.,

Sign in to comment.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!