Making a code runs faster

조회 수: 1 (최근 30일)
Ronaldo
Ronaldo 2013년 9월 2일
How can I make the following code runs faster (This a part of a larger code). I do not want to use parfor.
for rotation=1:360
RHO(:,1)=(CL*Landa)./T(tnum).LoAn(:,1);
THETA=T(tnum).LoAn(:,3);
X(:,1)=RHO.*cos(deg2rad(THETA(:,1)+rotation));
Y(:,1)=RHO.*sin(deg2rad(THETA(:,1)+rotation));
XP=round(72+X);
YP=round(72+Y);
mat=zeros(144,144);
for i=1:size(XP,1)
a=XP(i,1);
b=YP(i,1);
if a>0 & a<145 & b>0 & b<145
mat(a,b)= T(tnum).LoAn(i,2);
end
end
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 9월 2일
Isn't your RHO the same for each iteration? If so then compute it outside the loop. Same for THETA.
To confirm: LoAn is a 2D array, and not just a row vector ? so size(LoAn,1) > 1 ?
Roger Stafford
Roger Stafford 2013년 9월 2일
It looks as though your purpose in this code is to set up the array 'mat'. However, on each trip through the outer for-loop this array is completely erased and refilled with the information from the next 'rotation' angle. What is the sense in that? Only the last 360 degree angle would have any effect on the final values in 'mat'. Is it possible you are doing something with 'mat' which you haven't shown here before returning for another rotation angle? We need to know such things if we are to help you effectively.

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

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 2일
편집: Azzi Abdelmalek 2013년 9월 2일
  댓글 수: 1
Ronaldo
Ronaldo 2013년 9월 2일
In other part of the code, I did pre-allocating. I like to make more parallel calculations. Since I am going to use spmd, I try to avoid parfor loops.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by