How to optimize this matlab code?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Oliver Woodford
2015-4-16
A matrix multiply is basically an element-wise multiply followed by a sum, and these can both be vectorized.
采纳的回答
Oliver Woodford
2015-4-16
编辑:Oliver Woodford
2015-4-16
y = reshape(multiprod(multiprod(W_matrix, x), W_matrix'), [], K)';
It is an M-code file without for loops.
4 个评论
Oliver Woodford
2015-4-17
>> 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...
更多回答(1 个)
James Tursa
2015-4-15
编辑:James Tursa
2015-4-15
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 个评论
James Tursa
2015-4-15
编辑:James Tursa
2015-4-15
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.
Adam
2015-4-17
编辑:Adam
2015-4-17
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!