# How to optimize this matlab code?

1 view (last 30 days)

Show older comments

I am new to matlab and trying to optimize this matlab code through optimization

function X = DWT2DCT(x,Dct_matrix,W_matrix)

[M,N,K]=size(x);

y = zeros(K,M*N);

X = zeros(K,M*N);

temp = zeros(M,N);

for k=1:K

temp = W_matrix*x(:,:,k)*W_matrix';

y(k,:)=temp(:)';

end

X = Dct_matrix*y;

Till now I explored that it is possible through vectorization and this loop can be vectorized can someone please guide me

##### 8 Comments

Oliver Woodford
on 16 Apr 2015

### Accepted Answer

Oliver Woodford
on 16 Apr 2015

Edited: Oliver Woodford
on 16 Apr 2015

y = reshape(multiprod(multiprod(W_matrix, x), W_matrix'), [], K)';

It is an M-code file without for loops.

### More Answers (1)

James Tursa
on 15 Apr 2015

Edited: James Tursa
on 15 Apr 2015

I assume you are trying to vectorize this for speed purposes. In your current code, the x(:,:,k) expression copies data, and the y(k,:)=temp(:)' assignment copies data. Plus, the loop itself carries some overhead. This can be eliminated with a mex routine that calls the BLAS library directly, hence my question about variable sizes and C compiler (there are FEX submissions available that can do this calculation ... you would not have to write any C code yourself). Plus, part of the vectorization strategy would depend on the variable sizes involved. You have:

x is 8 x 8 x 10

W_matrix is 8 x 8

Dct_matrix is 10 x 10

These sizes are so small that I doubt there would be any significant speed increase gained with whatever method you choose, including mex routines.

If you don't want to consider mex then I would offer the following observations:

X = zeros(K,M*N); % useless line since you overwrite X later

temp = zeros(M,N); % useless line since you overwrite temp later

y = zeros(K,M*N);

:

y(k,:)=temp(:)';

Not the best way to organize your intermediate results in y. Storing results by row in y causes fragmented memory access to y at each iteration. It would be better to do something like this which will improve the memory access in y during iterations since it keeps the intermediate results contiguous in y:

y = zeros(M,N,K);

:

y(:,:,k) = W_matrix*x(:,:,k)*W_matrix';

Then after the loop reshape and transpose as needed for downstream processing.

y = reshape(y,M*N,K)';

Bottom line is that at the m-code level you cannot avoid those intermediate 2D slice copies since MATLAB does not support nD matrix multiply directly ... you need to use loops in some form.

FEX submissions that can do these types of nD matrix multiply calculations (some are mex and some are m-code):

MEX

(needs to be updated ... auto build does not work with later versions of MATLAB)

MEX

M-CODE

##### 4 Comments

Adam
on 17 Apr 2015

Abeera -> If you don't need to increase speed then unless you are wanting to do it as a learning exercise I'm not sure why you would bother.

The point of vectorised code is that it is faster than a loop. Often it is less readable, though some people find it more readable, but that is a semantic choice.

In general learning to vectorise code is very useful so doing it as a learning task has plenty of validity.

However, one of the most common misconceptions I see from people when it comes to Matlab is just the blanket idea that for loops are bad. Probably it is the second most common misconception after the idea that everything has to be done in a for loop! Both extremes of view are potentially spurious.

If you can write vectorised code straight off or convert a for loop quickly then of course it is good practice. However, basing such a decision purely on the idea that for loops are bad is wrong.

Get code to be correct first, then if you need to speed it up look to optimise it . If it is already fast enough then optimising it is something of a pointless task.

### See Also

### Community Treasure Hunt

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

Start Hunting!