# How to optimize this matlab code?

조회 수: 1(최근 30일)
Abeera Tariq 2015년 4월 15일
댓글: Abeera Tariq 2015년 4월 18일
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표시숨기기 이전 댓글 수: 7
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일
Use multiprod as follows:
y = reshape(multiprod(multiprod(W_matrix, x), W_matrix'), [], K)';
It is an M-code file without for loops.
##### 댓글 수: 4표시숨기기 이전 댓글 수: 3
Abeera Tariq 2015년 4월 18일

댓글을 달려면 로그인하십시오.

### 추가 답변(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표시숨기기 이전 댓글 수: 3
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.

댓글을 달려면 로그인하십시오.

### 범주

Find more on Multidimensional Arrays 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!

Translated by