I need to optimize my matlab code through vectorization

I need to vectorize this loop in code.. can someone guide?
function X = IDWT2DCT(x,Dct_matrix,IW_matrix)
[K,MN]=size(x);
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';
end
*****Speed is not issue, i just need to vanish this loop****** [EDITED, Jan, Code formatted]

댓글 수: 6

Please provide input data of the usual size e.g. created by rand. You want the vectorization to increase the speed of the function most likely. But then it matters if you operate on 100x100 matrices or on 100'000x1000.
Abeera Tariq
Abeera Tariq 2015년 4월 16일
편집: Abeera Tariq 2015년 4월 16일
I don't want to increase speed, i just want to vanish this loop.. speed does not matter for my prb.. loops does
well
x=rand(10,64);
Dct_matrix=rand(10,10);
IW_matrix=rand(8,8);
Sorry, I cannot imagine that a loop in the code can be a problem. If speed is not your problem, modifying a running code is simply a waste of time. Even with vectorized code the data are processed in loops internally also.
can u vectorize this code? @jan Simon?
I agree with Jan that the loop takes virtually no time, unless K is huge, and I'm talking billions. Looping a few hundred thousand or a few million times takes like a microsecond. What is the value of K? What are the dimensions of Y and x?
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..

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

답변 (1개)

Jan
Jan 2015년 4월 16일

1 개 추천

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

댓글 수: 2

I need to implement this on gpu nd there r many loops inside loops so i started removing these loops through vectorization.. :(
James Tursa
James Tursa 2015년 4월 16일
편집: James Tursa 2015년 4월 16일
If you are implementing this on a GPU then why not work with a BLAS library to do the matrix multiplies? E.g.,
https://developer.nvidia.com/cublas

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2015년 4월 16일

댓글:

2015년 4월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by