# How to optimize this matlab code?

1 view (last 30 days)
Abeera Tariq on 15 Apr 2015
Commented: Abeera Tariq on 18 Apr 2015
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

per isakson on 15 Apr 2015
"this loop can be vectorized" &nbsp How do you know? Most loops cannot.
Abeera Tariq on 15 Apr 2015
before posting question here i studies vectorization so i thought it may be vectorized as per my little knowledge
Oliver Woodford on 16 Apr 2015
A matrix multiply is basically an element-wise multiply followed by a sum, and these can both be vectorized.

Oliver Woodford on 16 Apr 2015
Edited: Oliver Woodford on 16 Apr 2015
Use multiprod as follows:
y = reshape(multiprod(multiprod(W_matrix, x), W_matrix'), [], K)';
It is an M-code file without for loops.

Show 1 older comment
Abeera Tariq on 17 Apr 2015
It did not work..
Oliver Woodford on 17 Apr 2015
>> x = randn(8, 8, 10); W_matrix = randn(8, 8);
>> y1 = reshape(multiprod(multiprod(W_matrix, x), W_matrix'), 64, 10)';
>> for a = 10:-1:1, y2(a,:) = reshape(W_matrix * x(:,:,a) * W_matrix', 1, []); end
>> isequal(y1, y2)
ans =
1
It works...
Abeera Tariq on 18 Apr 2015

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

Show 1 older comment
Abeera Tariq on 15 Apr 2015
I don't need to increase speed, i just want to exchange for loop with same logic
James Tursa on 15 Apr 2015
nD matrix multiply cannot be "vectorized" at the m-file level. MATLAB does not support nD matrix multiply directly, so you must use loops for this. The only way to avoid this is through the use of mex routines, which can bury the loops inside of C code (and not incur the 2D slice copy and variable creation penalties you get at the m-file level).
But again I would stress that your variable sizes are so small that you will likely not see any significant speed benefit even with a mex routine.