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!