how to reduce computation time for this loops?
이전 댓글 표시
m=n=512 Sd=3140; So= 2510; det=1120; A=zeros(360,det); cx=(n)/2;p=det/2; for L=1:1:360
for i=1:m
for j=1:n
Y=((So-cx)+j); %y-indices of pixel
if i<=cx
X=cx-i; % x-indices of pixel above r
D=p-((X/Y)*Sd);
end
if i>cx
X=i;
D=(((i-cx)/Y)*Sd)+p;
end
wy1=(floor(D)+1)-D;
wy2= 1-wy1;
ddata=A(L,:);
if (D) >1 && (D) <det
ima(i,j)=(ddata(1 ,floor(D) )*wy2)+(ddata(1 ,floor(D)+1 )*wy1);
end
end
end
댓글 수: 8
Optimizing the code requires to know the usual dimensions of the input data. So please post the values for m, n, So, cx, Sd, p, A, det. Providing test data, e.g. produced by RAND, would be even better, because we could run the code and test the effect of modifications.
Optimizing a program based on the code only without considering the processed data is a kind of blind guessing using magic crystal balls.
mohammed hegazy
2013년 5월 13일
Andrei Bobrov
2013년 5월 13일
? p
mohammed hegazy
2013년 5월 14일
mohammed hegazy
2013년 5월 15일
Matt Kindig
2013년 5월 15일
The first thing that I notice is that you should pre-allocate ima before your loops. Insert this line at the beginning of your script.
ima = NaN(m,n);
I'd bet that this alone will substantially improve your performance.
mohammed hegazy
2013년 5월 15일
Jan
2013년 5월 15일
@mohammed: Please do not mention such very important details is a comment, but this must appear in the original question, where readers expect it.
답변 (1개)
Roger Stafford
2013년 5월 15일
Here are some comments on your code:
1) As your code stands, the quantity 'L' has no effect whatever on the quantity 'D', so the condition "D>1&D<det" will hold true for the same set of i,j pairs for every value of L. This means that the final contents of 'ima' depend only on the last pass through that outer L-loop. The other 359 passes in the loop are totally wasted, with all their results being subsequently overwritten. I strongly suspect you actually have something quite different in mind here.
2) Where you have the condition i<=cx and i>cx, as you have written it, the result in D is the same in either case, namely
D = p-(cx-i)/(So-cx+j)*Sd
I suspect you meant to reverse the sign in one of these two cases, but if so instead of using the 'if' construct, you could just say
D = p - abs(cx-i)/(So-cx+j)*Sd
or
D = p + abs(cx-i)/(So-cx+j)*Sd
depending on whichever one it is you meant to have. Either way there is no need for the 'if' here.
3) As Matt Kindig has said, you need to pre-allocate the matrix 'ima' before entering the for-loops. In particular you need to decide what the default contents of 'ima' are to be when nothing from 'ddata' will have been entered, that is, an i,j combination for which D>1&D<det is false for every L.
4) As the code stands now using only L = 360, it can be entirely vectorized with all for-loops eliminated. Whether that would remain true with all values of L being involved in accordance with comment 1), would depend on the nature of the change made to accomplish that.
댓글 수: 3
mohammed hegazy
2013년 5월 15일
Roger Stafford
2013년 5월 15일
I don't think you understand the point I am making, Mohammed. The D values you compute are the same for each value of L as you have written the code. L doesn't enter into the computation of D. This means that whenever an i,j pair produces a D which satisfies the condition and allows ima(i,j) to be written to, then that same D value will be computed for every L value for this i,j pair, and that means only 'ddata' computations with L = 360 will have been left in ima(i,j) at the end. The other L values might just as well have been skipped. I am sure this is not what you intend to happen. What I am saying is that either the algorithm needs to change, or you only need to do the computation for L = 360.
mohammed hegazy
2013년 5월 22일
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!