Reduce execution time in a code having 'while' loop
이전 댓글 표시
I have written a code for finding galaxy pairs. Array 'v1' contains 8 columns in which each column contains different properties of galaxies. 'v1' has 93000 rows and 8 columns. The code is attached below:-
c=3*10^5;
H0=67.4;
f=@(x) (0.315.*(1+x).^3+0.685).^(-1/2);
i=1;j=0;
while i<=size(v1,1);
k=1;
while k<=size(v1,1);
if k~=i;
dvel=c*abs(v1(i,2)/(1+v1(i,2))-v1(k,2)/(1+v1(k,2)))
theta=acos(cosd(v1(i,4))*cosd(v1(k,4))*cosd(v1(i,3)-v1(k,3))+sind(v1(i,4))*sind(v1(k,4)));
r=(c/H0)*integral(f,0,0.5*(v1(i,2)+v1(k,2)));
rp=theta*r;
if rp<=0.15;
j=j+1;
pair(j,:)=[v1(i,1) v1(k,1)];
end
end
k=k+1;
end
i=i+1
end
(1)c,H0 and f are some declared constant and functions respectively.
(2) size(v1,1)=93000
(3) For each value of 'i' here, 'k' loop is running (93000-1) times and therefore considering all values of 'i' , 'i' loop is running entirely for 93000 X (93000-1) times which actually takes a huge huge amount of time. Can anyone please suggest how to reduce the execution time of this loop, it would be of great help?
댓글 수: 1
Walter Roberson
2021년 11월 10일
편집: Walter Roberson
2021년 11월 10일
Question: what do you do with dvel ? You compute it, but I do not see any use of it.
Also, is there any column that the data is sorted on? With some algorithms, sorting on the second column might help speed things up, potentially allowing us to cut out some of the integrations. Not sure yet it is worth the overhead.
Also, what order of magnitude are the values in the second column ?
채택된 답변
추가 답변 (1개)
Apashanka Das
2021년 11월 11일
0 개 추천
댓글 수: 3
Apashanka Das
2021년 11월 11일
Walter Roberson
2021년 11월 11일
What accuracy levels do you need? Some of your constants only have single digit precision, but some of them have 3 digits precision, and your rp boundary only has two digits precision.
The reason I ask is that over that range of values in column 2, the integral is pretty much a straight line, and could be replaced by a very low order polynomial, if you are willing to accept that the occasional point might not be perfectly classified if it is right on the boundary. Remembering that your speed of light is not exactly accurate anyhow...
Apashanka Das
2021년 11월 12일
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!