Asked by mohammed hegazy
on 13 May 2013

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

Answer by 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.

mohammed hegazy
on 15 May 2013

Roger Stafford
on 15 May 2013

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

Log in to comment.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 8 Comments

## Jan Simon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75564-how-to-reduce-computation-time-for-this-loops#comment_148597

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 (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75564-how-to-reduce-computation-time-for-this-loops#comment_148612

m=n=512 Sd=3140; So= 2510; det=1120; A=zeros(360,det); cx=(n)/2;

## Andrei Bobrov (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75564-how-to-reduce-computation-time-for-this-loops#comment_148617

?

p## mohammed hegazy (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75564-how-to-reduce-computation-time-for-this-loops#comment_148762

p=det/2

## mohammed hegazy (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75564-how-to-reduce-computation-time-for-this-loops#comment_148992

please i need the answer

## Matt Kindig (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75564-how-to-reduce-computation-time-for-this-loops#comment_148993

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.

I'd bet that this alone will substantially improve your performance.

## mohammed hegazy (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75564-how-to-reduce-computation-time-for-this-loops#comment_148997

i am already allocate it before for loop like that ima=zeros(m,n);

## Jan Simon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75564-how-to-reduce-computation-time-for-this-loops#comment_149046

@mohammed: Please do not mention such very important details is a comment, but this must appear in the original question, where readers expect it.

Log in to comment.