how to reduce computation time for this loops?

1 view (last 30 days)
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 Comments
mohammed hegazy
mohammed hegazy on 15 May 2013
i am already allocate it before for loop like that ima=zeros(m,n);
Jan
Jan on 15 May 2013
@mohammed: Please do not mention such very important details is a comment, but this must appear in the original question, where readers expect it.

Sign in to comment.

Answers (1)

Roger Stafford
Roger Stafford on 15 May 2013
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 Comments
Roger Stafford
Roger Stafford on 15 May 2013
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
mohammed hegazy on 22 May 2013
D is calculated to every i,j pairs and for the same pair of i,j i put the value of ((ddata(1 ,floor(D) )*wy2)+(ddata(1 ,floor(D)+1 )*wy1);) in the pixel of image with indices i,j
and after all i,j(all pixel) i rotate the image with value of L and repeat the above steps again and for each loop i add the output to previous output

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!