Matlab runs very slower (almost stucks) after some iteration in for loop
Afficher commentaires plus anciens
Dear All,
In my program I have a for loop which has to run 5823x8153 times. The for loop runs quite faster until i= 3944 and j = 8153. However, after this iteration the for loop is running very slowly almost it stucks at this point. I tried some acceleration methods like pre allocation and computing some variables before the for loop but it did not work. The problematic for loop is given as below. Could you please help me about this problem? By the way, it works good for small portions(1000x1000 or 3000x3000) of the same data and I run this program using a workstation with 64GB RAM. I am looking forward to hearing from you
ro1 = (180*3600)/pi;
ro = ro1^2;
sigma_h = horizontal_prec_secs;
sigma_v = vertical_prec_secs;
error_ellip_dim = nan(5823,8153,3);
for i = 1:5823
for j = 1:8153
sigma_r = range_precision(i,j);
h = horizontal_angle(i,j);
v = vertical_angle(i,j);
r = range(i,j)*1000;
if (isnan(h))||(isnan(sigma_r))
error_ellip_dim(i,j,:) = NaN;
else
EXX(1,1) = sigma_r^2*COSDH(i,j)^2*COSDV(i,j)^2 + (r^2*sigma_h^2*COSDV(i,j)^2*SINDH(i,j)^2)/ro + (r^2*sigma_v^2*COSDH(i,j)^2*SINDV(i,j)^2)/ro;
EXX(1,2) = sigma_r^2*COSDH(i,j)*COSDV(i,j)^2*SINDH(i,j) - (r^2*sigma_h^2*COSDH(i,j)*COSDV(i,j)^2*SINDH(i,j))/ro + (r^2*sigma_v^2*COSDH(i,j)*SINDH(i,j)*SINDV(i,j)^2)/ro;
EXX(1,3) = COSDH(i,j)*COSDV(i,j)*SINDV(i,j)*sigma_r^2 - (r^2*sigma_v^2*COSDH(i,j)*COSDV(i,j)*SINDV(i,j))/ro;
EXX(2,1) = EXX(1,2);
EXX(2,2) = sigma_r^2*COSDV(i,j)^2*SINDH(i,j)^2 + (r^2*sigma_h^2*COSDH(i,j)^2*COSDV(i,j)^2)/ro + (r^2*sigma_v^2*SINDH(i,j)^2*SINDV(i,j)^2)/ro;
EXX(2,3) = COSDV(i,j)*SINDH(i,j)*SINDV(i,j)*sigma_r^2 - (r^2*sigma_v^2*COSDV(i,j)*SINDH(i,j)*SINDV(i,j))/ro;
EXX(3,1) = EXX(1,3);
EXX(3,2) = EXX(2,3);
EXX(3,3) = sigma_r^2*SINDV(i,j)^2 + (r^2*sigma_v^2*COSDV(i,j)^2)/ro;
[eig_vec_mat,eig_val_mat] = eig(EXX);
ellip_params = sqrt(diag(eig_val_mat));
error_ellip_dim(i,j,:) = ellip_params';
end
end
i
j
end
2 commentaires
the cyclist
le 18 Jan 2017
Modifié(e) : the cyclist
le 18 Jan 2017
Can you upload values (e.g. in a MAT file) for all the variables that are not defined here, so that we can run your code?
mustafa ozendi
le 18 Jan 2017
Réponses (2)
Jan
le 24 Jan 2017
You can accelerate the code - although this was not your question:
ro1 = (180*3600)/pi;
ro = ro1^2;
sigma_h = horizontal_prec_secs;
sigma_v = vertical_prec_secs;
error_ellip_dim = nan(5823,8153,3);
for i = 1:5823
for j = 1:8153
sigma_r = range_precision(i,j);
h = horizontal_angle(i,j);
if ~isnan(h) && ~isnan(sigma_r) % No need to set other elements to NaN
v = vertical_angle(i,j);
r = range(i,j)*1000;
sigma_r2 = sigma_r^2; % Constants to avoid repeated calculations
COSDV2 = COSDV(i,j)^2;
SINDV2 = SINDV(i,j)^2;
r2 = r^2;
SINDH2 = SINDH(i,j)^2;
COSDH2 = COSDH(i,j)^2;
r2sigma_h2 = r2*sigma_h^2;
r2sigma_v2 = r2*sigma_v^2;
EXX(1,1) = sigma_r2 * COSDH2*COSDV2 + (r2sigma_h2*COSDV2*SINDH2)/ro + (r2sigma_v2*COSDH2*SINDV2)/ro;
EXX(1,2) = sigma_r2*COSDH(i,j)*COSDV2*SINDH(i,j) - (r2sigma_h2*COSDH(i,j)*COSDV2*SINDH(i,j))/ro + (r2sigma_v2*COSDH(i,j)*SINDH(i,j)*SINDV2)/ro;
EXX(1,3) = COSDH(i,j)*COSDV(i,j)*SINDV(i,j)*sigma_r2 - (r2sigma_v2*COSDH(i,j)*COSDV(i,j)*SINDV(i,j))/ro;
EXX(2,1) = EXX(1,2);
EXX(2,2) = sigma_r2*COSDV2*SINDH2 + (r2sigma_h2*COSDH2*COSDV2)/ro + (r2sigma_v2*SINDH2*SINDV2)/ro;
EXX(2,3) = COSDV(i,j)*SINDH(i,j)*SINDV(i,j)*sigma_r2 - (r2sigma_v2*COSDV(i,j)*SINDH(i,j)*SINDV(i,j))/ro;
EXX(3,1) = EXX(1,3);
EXX(3,2) = EXX(2,3);
EXX(3,3) = sigma_r2*SINDV2 + (r2sigma_v2*COSDV2)/ro;
[eig_vec_mat,eig_val_mat] = eig(EXX);
error_ellip_dim(i,j,:) = sqrt(diag(eig_val_mat)).'; % ' or .' ?!
end
end
% Output to command window might be time consuming - removed: "i j"
end
I curious: Does avoiding the bunch of square operations speed up the code measurably? Most likely the eig() will be the bottleneck, so perhaps this suggestion is not important. I don't know if Matlab calculates the eigenvectors of 3x3 matrices efficiently. See: https://www.geometrictools.com/Documentation/RobustEigenSymmetric3x3.pdf and https://en.wikipedia.org/wiki/Eigenvalue_algorithm#3.C3.973_matrices .
1 commentaire
mustafa ozendi
le 24 Jan 2017
mustafa ozendi
le 24 Jan 2017
0 votes
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!