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

Learn moreOpportunities for recent engineering grads.

Apply Today
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

i am agree with you in the second point but first point i need this condition to remove values out this condition so i can't remove this condition

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 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

## 8 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75564#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.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75564#comment_148612

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

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75564#comment_148617

?

pDirect link to this comment:http://www.mathworks.com/matlabcentral/answers/75564#comment_148762

p=det/2

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75564#comment_148992

please i need the answer

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75564#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.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75564#comment_148997

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

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75564#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.