Hello, can anyone solve this preallocation problem in my code?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mohammad Shafi Nikzada
le 10 Juin 2021
Réponse apportée : DGM
le 12 Juin 2021
Although I tried to preallocate Matrix of zeros still the warning is there and the result changes when I preallocate. Warnings are in line 101,103,105.
0 commentaires
Réponse acceptée
DGM
le 12 Juin 2021
You had allocated an empty vector and were growing it by concatenation. It works, but is often slower. Since you know the size of the vectors, just preallocate them at their final size and assign the values by indexing.
% Calculation of rotation matrixes
npos = numel(ERA);
GCRS_X = zeros(npos,1);
GCRS_Y = zeros(npos,1);
GCRS_Z = zeros(npos,1);
for i=1:npos
R= Rotation_3(-ERA(i));
R2= Rotation_2((Xp(i)./3600) *pi/180);% conversion of arcsecond to radians
R1= Rotation_1((Yp(i)./3600) *pi/180);% conversion of arcsecond to radians
W = Rotation_3(-((s(i)./3.6e9)*pi/180))*R2*R1;
Trs= R*W;
% Transformation of ITRS positions to inertial GCRS positions
GCRS_X(i) = Trs(1,1)*x(i)+Trs(1,2)*y(i)+Trs(1,3)*z(i); %GCRS_X in Meter
GCRS_Y(i) = Trs(2,1)*x(i)+Trs(2,2)*y(i)+Trs(2,3)*z(i); %GCRS_Y in Meter
GCRS_Z(i) = Trs(3,1)*x(i)+Trs(3,2)*y(i)+Trs(3,3)*z(i); %GCRS_X in Meter
end
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!